Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “通过facebook api循环”;“喜欢”;_Php_Arrays_Facebook_Facebook Php Sdk - Fatal编程技术网

Php “通过facebook api循环”;“喜欢”;

Php “通过facebook api循环”;“喜欢”;,php,arrays,facebook,facebook-php-sdk,Php,Arrays,Facebook,Facebook Php Sdk,嘿,我正在努力得到所有喜欢的PHP墙柱。阵列框架如下所示: Array ( [data] => Array ( [0] => Array ( [id] => XXXXXXXX_XXXXXXXXXXX [from] => Array ( [name] => Bob Barker [id] => XXXXXXXXXXX ) [mess

嘿,我正在努力得到所有喜欢的PHP墙柱。阵列框架如下所示:

Array ( 
[data] => Array ( 
    [0] => Array ( 
        [id] => XXXXXXXX_XXXXXXXXXXX 
        [from] => Array ( 
            [name] => Bob Barker
            [id] => XXXXXXXXXXX
        ) 
        [message] => This is a message here!!! 
        [story] => We shared a message with others
        [story_tags] => Array ( 
            [0] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXX 
                    [name] => Bob Barker
                    [offset] => 0 
                    [length] => 11 
                    [type] => user 
                ) 
            ) 
            [19] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXXXXXXXXXXXXXXX 
                    [name] => NAME 
                    [offset] => 19 
                    [length] => 5 
                    [type] => page 
                ) 
            ) 
        ) 
        [picture] => [removed] 
        [link] => [removed]
        [name] => Timeline Photos 
        [caption] => This is just a caption for the post here 
        [properties] => Array ( 
            [0] => Array ( 
                [name] => By 
                [text] => NAME 
                [href] => [removed] 
            ) 
        ) 
        [icon] => https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif 
        [actions] => Array ( 
            [0] => Array ( 
                [name] => Like 
                [link] => [removed]
            ) 
        ) 
        [privacy] => Array ( 
            [value] => 
        ) 
        [type] => photo 
        [status_type] => shared_story 
        [object_id] => XXXXXXXXXXXXXXXX
        [application] => Array ( 
            [name] => Photos 
            [id] => XXXXXXXXXX 
        ) 
        [created_time] => 2014-02-12T21:33:17+0000 
        [updated_time] => 2014-02-12T21:33:17+0000 
        [likes] => Array ( 
            [data] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXXXX 
                    [name] => John Doe 
                ) 
                [1] => Array ( 
                    [id] => XXXXXXXXXXXX 
                    [name] => Steve Doe 
                ) 
            ) 
            [paging] => Array ( 
                [cursors] => Array ( 
                    [after] => XXXXXXXXXXXXXXXX 
                    [before] => XXXXXXXXXXXX== 
                ) 
            ) 
        ) 
    ) 
我当前的PHP代码是:

$feed = $facebook->api('/me/home'); //This is the array above

foreach($feed['data'] as $post) {
  $story = (isset($post['story']) ? $post['story'] : null);
  $id = (isset($post['id']) ? $post['id'] : null);
  $name = (isset($post['name']) ? $post['name'] : null);
  $message = (isset($post['message']) ? $post['message'] : null);
  $post_link = (isset($post['actions'][0]['link']) ? $post['actions'][0]['link'] : null);
  $picture = (isset($post['picture']) ? $post['picture'] : null);

  foreach($post['likes'] as $liked) {
      echo $post['id'];
  }

  echo $name . ' ' . $story . ' it was: ' . $message . ' and ' . $post_link .  ' and <img src="' . $picture . '" /><br />';
  echo '=======================================================================================================';
}
似乎没有按原样工作。为了让它在数组中的所有“喜欢的”帖子中循环,我缺少了什么

foreach($post['likes']->data as $like) {
  echo $like->id;
}

从那以后就不起作用了

$post['likes']
元素也是一个嵌套数组,如下所示:

likes => array => data => array=> ..
所以你需要做循环,就像

if(array_key_exists('data',$post['likes'])){
        foreach($post['likes']['data'] as $liked=>$val) {
            echo '<br />Like ID: ' .$val["id"].'<br />' ;
        }
    }
if(数组\键\存在('data',$post['likes')){
foreach($post['likes']['data']作为$liked=>$val){
回显“
类似ID:”.$val[“ID”]。
; } }
在执行循环之前,最好先检查数组键是否存在,这样,如果返回的数据不包含该键,就不会出现未定义的索引错误

likes => array => data => array=> ..
if(array_key_exists('data',$post['likes'])){
        foreach($post['likes']['data'] as $liked=>$val) {
            echo '<br />Like ID: ' .$val["id"].'<br />' ;
        }
    }