我从示例sdk facebook获得了这个简单的代码:
if ($user) {
try {
$user_profile = $facebook->api('/me/friends');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
如果我打印$ user_profile,结果就是这种格式的朋友列表:
[206] => Array
(
[name] => name
[id] => 00000000
)
是否可以访问这些数组的字段? 例如,我想只打印朋友的名字
答案 0 :(得分:0)
要仅打印朋友的名字,只需遍历主数组/对象:
foreach($user_profile as $k => $friend)
{
echo $friend['name'].' - '.$user_profile[$key]['id'].'<br/>';
}
访问特定朋友:
echo $user_profile[11]['name'];
这与数组中第11位朋友的名字相呼应。将两者结合起来使用函数(或方法)获取给定朋友的索引:
public function getFriendIndex($name)
{
foreach($this->user_profile as $index => $friend)
{
if ($friend['name'] === $name)
{//returns the index of $name's friend data
return $index;
}
}
return null;//friend doesn't exist...
}