douzhoubing2805 2014-09-01 06:25
浏览 20
已采纳

Facebook PHP 4.x返回数组解析

Hey all I am just now getting around to the newest version of the PHP sdk for Facebook. It seems to use a new array of returned data that I can not seem to be able to parse out what I need and not all other gibberish it has contained inside the array.

The array returned looks like this:

Array (
    [data] => Array (
        [0] => stdClass Object (
            [id] => 439676913452345546787863523525
            [name] => John Doe
            [picture] => stdClass Object (
                [data] => stdClass Object (
                    [is_e] =>
                    [url] => https://blah.com....
                )
            )
        )
        [1] => stdClass Object (
            [id] => 56594790468026754634524674
            [name] => Bob Barker
            [picture] => stdClass Object (
                [data] => stdClass Object (
                    [is_e] =>
                    [url] => https://blah.com....
                )
            )
        )
        [2]...etc etc...
        [paging] => stdClass Object ( 
            [cursors] => stdClass Object ( 
                [before] => QWFMmhllXN5JH....Rk52QWc= 
                [after] => WGHtdnNIwaDlRz....05RNB0E= 
            ) 
        )
    )
)

In the older version of the Facebook SDK I just needed to do something along these lines (below code example is getting the id within the returned array and checking also to make sure it has a value or not):

foreach($theReturnedArray['data'] as $rData) {
  $id = (isset($rData['id']) ? $rData['id'] : null);
  $name = (isset($rData['name']) ? $rData['name'] : null);

  echo $name . ' ' . $id . '<br />';
}

in order to get the value that I needed. Now the return has an added stdClass object within the array itself and I am unsure on how to go about getting id, name & picture url from it.

Any help would be awesome!

ANSWER

It was caused of an error in my code that I was getting 0's back (the +):

 echo $name . ' ' . $id + '<br />';

And I needed to change it to:

 echo $name . ' ' . $id . '<br />';

and the full code together:

 foreach($graphObject['data'] as $rData) {
    $id = (isset($rData->id) ? $rData->id : null);
    $name = (isset($rData->name) ? $rData->name : null);
    $picUrl = (isset($rData->picture->data->url) ? $rData->picture->data->url : null);

    echo $id . '<br/>';
    echo $name . '<br/>';
    echo $picUrl . '<br/>';
    echo '======================================<br/>';
 }
  • 写回答

1条回答 默认 最新

  • douzhonglong3789 2014-09-01 06:26
    关注

    You get values from objects with ->

    echo $rData->id;     // This format is for objects
    

    And not

    echo $rData['id'];   // This format is for arrays
    

    So your code would now look like

    $id = isset($rData->id) ? $rData->id : NULL;
    $name = isset($rData->name) ? $rData->name : NULL;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line