3

我需要帮助尝试获取数组和 stdClass 对象中的值。

仅供参考:我正在使用foursquare API ...

我有这样的事情:

`$fsq_groups = $venues->response->groups;`

    echo "<pre>";
    print_r($fsq_groups);
    echo "</pre>";

print_r($fsq_groups);

Array
(
    [0] => stdClass Object
        (
            [type] => Recommended Places
            [name] => recommended
            [items] => Array
                (
                    [0] => stdClass Object
                        (
                            [reasons] => stdClass Object
                                (
                                    [count] => 2
                                    [items] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [type] => general
                                                    [message] => Lots of people have mentioned "burgers" here
                                                )

                                            [1] => stdClass Object
                                                (
                                                    [type] => general
                                                    [message] => This spot is popular on Foursquare
                                                )

                                        )

                                )

                            [venue] => stdClass Object
                                (
                                    [id] => 4afe5a4bf964a520372f22e3
                                    [name] => Stout Burgers & Beer
                                    [contact] => stdClass Object
...

我怎么去name下面venue

我尝试了这样的事情,但没有奏效:

foreach($fsq_groups as $groups)
{
    foreach($groups->items->venue as $venue)
    {

            echo $venue->name;
                echo "<pre>";
                print_r($venue);
                echo "</pre>";
    }
}
4

3 回答 3

5
$fsq_groups = $venues->response->groups;
if(isset($fsq_groups[0])){
    $groups = $fsq_groups[0];
    foreach($groups->items as $key => $place){
        $venue = $place->venue;
        echo $venue->name;
    }
}
于 2012-11-14T09:59:27.077 回答
2
foreach($fsq_groups as $groups)
{
    foreach($groups->items as $item)
    {

            echo $item->venue->name;
                echo "<pre>";
                print_r($item);
                echo "</pre>";
    }
}

试试看.. 物品是数组,所以你不能使用“items->venue”去场地

于 2012-11-14T09:01:38.703 回答
1

请试试这个,

foreach($fsq_groups as $key=>$groups)
{
    foreach($key->$groups as $venue)
    {

            echo $venue->name;
                echo "<pre>";
                print_r($venue);
                echo "</pre>";
    }
}
于 2012-11-14T09:02:42.507 回答