我正在使用foreach循環從Facebook中的圖API獲取圖像。如何從數組中排除對象,以便foreach循環避免它?
這裏是我的JSON解碼的回報:
{
"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=..."
}
}
這裏是我的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>';
}
}
}
這很好,因爲它拉動的路徑正確的圖像,但它給了我這個錯誤:
Notice: Trying to get property of non-object in C:\inetpub\wwwroot\feref.com\facebook\test-app\friends.php on line 50
我認爲這是數組中「分頁」對象的結果。
如何防止此錯誤?也就是說,我如何從循環中排除「分頁」對象?
賓果,歡呼聲的好友 – reekogi 2012-03-22 14:51:11