PHP foreach数组与stdClass对象。

时间:2022-10-23 07:59:18

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

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

FYI: I'm using foursquare API...

我正在使用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);

从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>";
    }
}

3 个解决方案

#1


4  

$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


0  

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


0  

Please try this,

请试试这个,

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

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

#1


4  

$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


0  

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


0  

Please try this,

请试试这个,

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

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