douqiang4245 2012-03-22 14:45
浏览 152
已采纳

如何从数组中排除一个对象,以便foreach循环避免它?

I'm using a foreach loop to get images from the graph API in facebook.

Here is the return from my json decode:

{
   "data": [
      {
         "name": "Person 1",
         "id": "12345678",
         "picture": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/123456789_123456_789456.jpg"
      },
      {
         "name": "Person 2",
         "id": "12345679",
         "picture": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/123456789_123456_789457.jpg"
      }
   ],
   "paging": {
      "next": "https://graph.facebook.com/me/friends?access_token=.....&fields=name,id,picture&limit=5000&offset=5000&__after_id=..."
   }
}

Here's my PHP:

        $url  = "https://graph.facebook.com/". $uid ."/friends?access_token=". $access_token ."&fields=name,id,picture";
        $users = json_decode(file_get_contents($url));

        echo $access_token .'<br><br>';

        foreach ($users as $name => $value) {
            foreach ($value as $entry) {
                //Loop for profile pics
                if ($entry->picture) {
                    echo '<a href="https://www.facebook.com/profile.php?id=' . $entry->id . '" target="_blank">' . '<img src="'. $entry->picture . '" class="profile-thumb' . $entry->name . '" id="'. $entry->id .'"></a>';
                }
            }
        }

This is fine as it pulls the paths to the images correctly, but it gives me this error:

Notice: Trying to get property of non-object in C:\inetpub\wwwroot\feref.com\facebook\test-app\friends.php on line 50

Which I assume is as a result of the 'paging' object in the array.

How can I prevent this error? That is, how can I exclude the 'paging' object from my loop?

  • 写回答

2条回答 默认 最新

  • douxunzui1519 2012-03-22 14:49
    关注

    Try if (isset($this->picture)) instead. That'll test if the picture property exists at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类