PHP foreach array with stdClass Object

送分小仙女□ 提交于 2019-12-11 01:40:43

问题


I need help trying to get values inside array and stdClass Object.

FYI: I'm using foursquare API...

I have something like this:

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

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

From 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
...

How do I get to the name under venue

I tried something like this but didn't work:

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

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

回答1:


$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;
    }
}



回答2:


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

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

Try that.. Items is array so you cant go to venue using "items->venue"




回答3:


Please try this,

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

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


来源:https://stackoverflow.com/questions/13375691/php-foreach-array-with-stdclass-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!