从PHP Array检索数据时出错

时间:2014-12-16 05:27:31

标签: php arrays

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

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类型的对象作为数组

2 个答案:

答案 0 :(得分:0)

使用

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

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

答案 1 :(得分:-1)

试试这个:

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