stdClass object array inside of another array

青春壹個敷衍的年華 提交于 2019-12-13 11:35:15

问题


I'm trying to get information from the $array1 below.

I'm getting with no problems venue's name and location address by doing:

$array2 = array();
$array3 = array();
foreach($array1 as $item){

                 $array2[] = $item->venue->name;
                 $array3[] = $item->venue->location->address;
}

But now I need to get the photos url and I don't know how to do it.

Thanks a million!

$array1:

Array
(
    [0] => stdClass Object
        (
               [venue] => stdClass Object

                (

                    [name] => a name
                    [location] => stdClass Object
                        (
                            [address] => main street

                        )


                )

            [photos] => stdClass Object
                (
                    [count] => 1
                    [items] => Array
                        (
                            [0] => stdClass Object
                                (

                                    [url] => http://folder/photo1.jpg

                    .
                    .
                    )))
.
.

回答1:


$array1[0]->photos->items[0]->url

Remember - you access arrays with [index] parenthesis, objects with -> arrows.




回答2:


Untested code:

$array2 = array();
$array3 = array();
$photos = array();
foreach($array1 as $item){
    $array2[] = $item->venue->name;
    $array3[] = $item->venue->location->address;

    $item_photo_urls = array();
    foreach($item->photos->items as $photo){
        $item_photo_urls[] = $photo->url;
    }

    $photos[] = $item_photo_urls;
}

Now you have a third array called photos , which contains all the photo urls.




回答3:


Try this :

$url = $item->photos->items[0]->url;


来源:https://stackoverflow.com/questions/6099068/stdclass-object-array-inside-of-another-array

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