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条)

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数