1

可能重复:
php 循环遍历关联数组

我有一个打印数组名称 $friends 的变量,它的输出是这样的:

{
  "data": [
    {
      "name": "Penelope-ns-test",
      "category": "Community",
      "id": "187099821418102",
      "created_time": "2012-06-14T15:00:59+0000"
    },
    {
      "name": "Fabric",
      "category": "Computers/technology",
      "id": "194407767268061",
      "created_time": "2012-06-07T07:24:03+0000"
    },

ETC...

我需要做一个循环才能得到这样的东西:if friends[id] == 1203438484 {

}

别的...

foreach($friends['data'] as $data){
                $fbid = $data['id'];
                $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
                $fbname = $data['name'];
                $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
                $image = theme('image', array('path' => $path, 'alt' => $fbname));
                $return .= '<div class="fimage">'.$image.'</div>';
                $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';
                foreach ($fbfriendlikes["data"] as $fbfriendlikes) {
    if ($fbfriendlikes["id"] == 184759054887922) {

                $return .= '<div class="flink">'.$link.'</div>';
                break;
                }
                }
            }

你能帮我写代码吗?谢谢!

4

2 回答 2

3
foreach ($friends["data"] as $friend) {
    if ($friend["id"] == 12345) {
        // Do something with $friend

        break;
    }
}
于 2012-06-25T18:16:45.240 回答
2
foreach($array['data'] as $friend)
{
    if ($friend['id'] == 1203438484)
    {
        //do something
    }

}
于 2012-06-25T18:17:34.967 回答