从 PHP 数组检索数据时出错


Error in retrieving data from PHP Array

我正在为 php 开发facebook sdk 4.0。 我在数组中检索了朋友列表,但我无法访问数组中的图片。 数组格式如下:

Array
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => AaJvu1z_MAA0INV57e4Bg5hOCaJDzObdptiArw
                    [name] => Mr. John
                    [picture] => stdClass Object
                        (
                            [data] => stdClass Object
                                (
                                    [is_silhouette] => 
                                    [url] => https://fbprofile-a.akamaihd.net/picture.jpg
                                )
                        )
                )
)

我的PHP代码是:

foreach ($friend_list['data'] as $friends) {
    $id = $friends->id;
    $name = $friends->name;
    $picture = $friends['data']->picture;
    echo "ID: ".$id."<br>";
    echo "Name: ".$name."<br>";
    echo "Picture: ".$picture."<br>";
}
"

ID"和"名称"工作正常。 但是"图片对象"中存在错误:

错误:不能将 stdClass 类型的对象用作数组

使用

$picture = $friends->picture->data->url;

当您使用对象检索时,您的顺序也是错误的。

试试这个:

<?php
foreach ($friend_list['data'] as $friends) {
    $id = $friends->id;
    $name = $friends->name;
    $picture = $friends->picture->data->url;
    echo "ID: ".$id."<br>";
    echo "Name: ".$name."<br>";
    echo "Picture: ".$picture."<br>";
}