2012-04-15 32 views
1

我正在設計一個應用程序,該應用程序將受益於用戶的Facebook朋友在他們鍵入時向他們提示。我遇到的困難是將Open Graph結果(通過graph.facebook.com/me訪問用戶朋友時)轉換爲AutoSuggest jQuery插件所需的格式。重新格式化從Facebook解碼的PHP陣列打開圖形

我知道我可以通過使用$user = json_decode(file_get_contents($graph_url));將此JSON結果解碼爲一個數組,但是我沒有足夠的經驗來使用PHP訪問此數組的部分並將其放入以下格式。

需要

格式:["First Friend","Second Friend","Third Friend"]

當前格式:([data] => Array ([0] => ([name] => Ryan Brodie [id] => 740079895)) [paging] => ([next] => https://graph.facebook.com/me/friends?access_token=ACCESS_TOKEN&limit=5000&offset=5000&__after_id=USERID))

感謝的提前對您有所幫助。

回答

0

Zombat的答案几乎是正確

$user = json_decode(file_get_contents($graph_url)); 
$friends = array(); 
foreach($user->data as $friend) { 
    $friends[] = $friend->name; 
} 

echo json_encode($friends); 
+0

這給了我同樣的錯誤,「不能使用類型爲stdClass的對象作爲數組」? – 2012-04-15 20:13:46

+0

我將$ friend ['name']更改爲$ friend-> name以查看是否可以糾正錯誤,並返回[False,False,False]? – 2012-04-15 20:15:47

+0

修復它我自己,偉大的東西,謝謝! – 2012-04-15 20:17:10

0
$user = json_decode(file_get_contents($graph_url)); 
$friends = array(); 
foreach($user['data'] as $friend) { 
    if (! empty($friend['name'])) { 
    $friends[] = $friend['name']; 
    } 
} 

print_r($friends); 
+0

這是給錯誤:'()提供的foreach無效參數' – 2012-04-15 20:00:58

+0

忽略的是,實際的錯誤是「不能使用類型爲stdClass的對象爲陣」 ? – 2012-04-15 20:06:03