1

Possible Duplicate:
php loop through associative arrays

i have a variable which prints an array names $friends, the output of it is something like this:

{
  "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...

I need to do a loop in order to get something like this : if friends[id] == 1203438484 {

}

else...

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;
                }
                }
            }

Can you help me with the code? Thanks!

0

2 Answers 2

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

        break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please see my updated code? i am inserting it like this and i see no output.. $friends has somehow the same structure as fbfriendlikes..
foreach ($fbfriendlikes["data"] as $fbfriendlikes) { -- you are using the same variable as your data source and your iteration variable. Try foreach ($fbfriendlikes["data"] as $fbfriendlike) { and adjust the variable name in the block accordingly.
Can you please do me one favor, just based on my code substitute what i need to substitute, i know i'll do any error, i mean which variable should i adjust... i'm just beginning to learn php. sorry for being dumb
2
foreach($array['data'] as $friend)
{
    if ($friend['id'] == 1203438484)
    {
        //do something
    }

}

2 Comments

Ahh cdhowie just beat me to it! :)
Can you please see my updated code? i am inserting it like this and i see no output.. $friends has somehow the same structure as fbfriendlikes..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.