Jump to content

Accessing and processing 3rd level JSON array through php


frobak

Recommended Posts

i

 

Im making a facebook app and i need to access the 3rd level data in a JSON array, ive managed to output the first 2, but then im getting an invalid argument.

 

I want to replicate the wall function

 

The below code outputs the top 2 arrays no problem

 

$feeds = $facebook->api('/255500227799817/feed');

foreach($feeds['data'] as $feed => $list) {
    echo "<h2>$feed</h2><ul>";
    foreach($list as $k => $v) {
            echo "<li>$k - $v</li>\n";
    }
    echo "</ul>";
}

 

this is the output

 


    id - 255500227799817_255522881130885
    from - Array
    message - just another status update
    actions - Array
    type - status
    created_time - 2011-07-13T10:57:13+0000
    updated_time - 2011-07-13T10:57:13+0000

 

So i need to access and display the 'from' array and the 'actions' array

 

Can i do this by amending the above php function?

yes sorry thats what im trying to do, have a third foreach loop inside. but i cant get it to work.

 

i split the first array into $feed(key) and $list(value)

 

and then i split the second array $list into $k(key) and $v(value)

 

but how do i get the 3rd array, because if i follow the logic there i split $v but its the value not the array??

 

I saw from your post about adding the ['from'] but couldnt get it to work even with just that as a single loop

 

confused!

below is the json of a single one and the printr below. I just need to capture every value, also the actions which look like another array

 

   "data": [
      {
         "id": "255500227799817_255522881130885",
         "from": {
            "name": "Frobaks Bits",
            "id": "255500227799817"
         },
         "message": "just another status update",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/255500227799817/posts/255522881130885"
            },
            {
               "name": "Like",
               "link": "http://www.facebook.com/255500227799817/posts/255522881130885"
            }
         ],
         "type": "status",
         "created_time": "2011-07-13T10:57:13+0000",
         "updated_time": "2011-07-13T10:57:13+0000"
      },

 

print_r($feeds["data"]);

 

Array ( [0] => Array ( [id] => 255500227799817_255522881130885 [from] => Array ( [name] => Frobaks Bits [id] => 255500227799817 ) [message] => just another status update [actions] => Array ( [0] => Array ( [name] => Comment [link] => http://www.facebook.com/255500227799817/posts/255522881130885 ) [1] => Array ( [name] => Like [link] => http://www.facebook.com/255500227799817/posts/255522881130885 ) ) [type] => status [created_time] => 2011-07-13T10:57:13+0000 [updated_time] => 2011-07-13T10:57:13+0000 ) [1] => Array ( [id] => 255500227799817_255522584464248 [from] => Array ( [name] => Frobaks Bits [id] => 255500227799817 ) [message] => Nice [picture] => http://photos-c.ak.fbcdn.net/hphotos-ak-ash4/284503_255522561130917_255500227799817_1243705_4977710_s.jpg [link] => http://www.facebook.com/photo.php?fbid=255522561130917&set=a.255522557797584.83739.255500227799817&type=1 [name] => Wall Photos [icon] => http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif [actions] => Array ( [0] => Array ( [name] => Comment [link] => http://www.facebook.com/255500227799817/posts/255522584464248 ) [1] => Array ( [name] => Like [link] => http://www.facebook.com/255500227799817/posts/255522584464248 ) ) [type] => photo [object_id] => 255522561130917 [created_time] => 2011-07-13T10:56:36+0000 [updated_time] => 2011-07-13T10:56:36+0000 [likes] => Array ( [data] => Array ( [0] => Array ( [name] => Alan Smith [id] => 869830416 ) ) [count] => 1 ) ) [2] => Array ( [id] => 255500227799817_255520917797748 [from] => Array ( [name] => Frobaks Bits [id] => 255500227799817 ) [message] => a [actions] => Array ( [0] => Array ( [name] => Comment [link] => http://www.facebook.com/255500227799817/posts/255520917797748 ) [1] => Array ( [name] => Like [link] => http://www.facebook.com/255500227799817/posts/255520917797748 ) ) [type] => status [created_time] => 2011-07-13T10:51:12+0000 [updated_time] => 2011-07-13T10:51:12+0000 ) ) 

 

 

 

 

Example:

foreach ($feeds["data"] as $feed) {
    echo $feed->from->name, " did something:
\n";
    foreach ($feed->actions as $action) {
        echo $action->name, ": ", $action->link, "
\n";
    }
}

Pretty much everything in there is either an object or an array of objects.

...I knew I was tired but I figured I could do this anyways.

 

Looking closer at the print_r() dump, it seems the API has decided you want an array of arrays, not an array of objects like json_decode() normally returns.

foreach ($feeds["data"] as $feed) {
    echo $feed["from"]["name"], " did something:
\n";
    foreach ($feed["actions"] as $action) {
        echo $action["name"], ": ", $action["link"], "
\n";
    }
}

Or, you could use something like:

$feeds = $facebook->api('/255500227799817/feed');

foreach($feeds['data'] as $feed => $list) {
    echo "<h2>$feed</h2><ul>";
    foreach($list as $k => $v) {
         echo "<li>$k - ";
        if(is_array($v)) { //if $v is an array.
           echo '<ul>'; //make a sub UL
          foreach($v as $kk => $vv) { //loop the array.
                 echo "<li>$kk - $vv</li>\n"; //list it.
           }
          echo '</ul>' . "\n"; //close it.
         } else { //if it ISN"T an array.
            echo $v; //just print it.
         }
         echo "</li>\n";
    }
    echo "</ul>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.