另一个数组中的stdClass对象数组


stdClass object array inside of another array

我正试图从下面的$array1中获取信息。

我通过做可以毫无问题地获得场地的名称和位置地址

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

但现在我需要得到照片的网址,我不知道怎么做。

万分感谢!

$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
                    .
                    .
                    )))
.
.
$array1[0]->photos->items[0]->url

请记住,您使用[index]括号访问数组,使用->箭头访问对象。

未测试代码:

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

现在您有了第三个名为photos的数组,它包含所有的照片url。

试试这个:

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