我正在设计一个应用程序,该应用程序将受益于用户的Facebook朋友在他们键入时向他们推荐。我遇到的困难是将Open Graph结果(通过 graph.facebook.com/me 访问用户朋友时)转换为AutoSuggest jQuery插件所需的格式。
我知道我可以使用$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 ) )
提前感谢您的帮助。
Zombat的答案几乎是正确的
$user = json_decode(file_get_contents($graph_url));
$friends = array();
foreach($user->data as $friend) {
$friends[] = $friend->name;
}
echo json_encode($friends);
$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);