frobak Posted July 13, 2011 Share Posted July 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/ Share on other sites More sharing options...
djlee Posted July 13, 2011 Share Posted July 13, 2011 if($k == 'from') Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242371 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 // either foreach ($feeds["data"]["from"] as $whatever) { // array // or foreach ($feeds["data"]->from as $whatever) { // object Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242382 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 Hi so how would i incorporate that into the script, would i replace the foreach($feeds['data'] as $feed => $list) { with foreach ($feeds["data"]->from as $whatever) { I cant seem to get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242408 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 You're supposed to look at the code, understand what it does, and make adjustments to your code so the two agree. You aren't supposed to copy and paste it. Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242410 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 ok fair point. ive been looking at this small script for 2 days but ill get back on it Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242428 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 i totally understand what the code does, thats easy, but the whole problem is extracting it in the same loop as the other arrays Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242437 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 They're different arrays. With different types of data. Why would you want to use one loop for everything? Loop over the "data". Inside that, loop over the "from" and/or "actions" separately. Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242442 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242448 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 Eh, I think I misunderstood what was in $feeds. What does print_r($feeds["data"]); output and what parts do you want from it? Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242451 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242455 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242467 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 thanks for your help and paitence. i take it i need to play with that to get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242469 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 Define "play". It probably doesn't do exactly what you want, but it shows how to get the various actions. Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242470 Share on other sites More sharing options...
frobak Posted July 13, 2011 Author Share Posted July 13, 2011 well i get this error: Warning: Invalid argument supplied for foreach() on this line foreach ($feed->actions as $action) { so i need to play around with this line Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242471 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 ...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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242482 Share on other sites More sharing options...
jcbones Posted July 14, 2011 Share Posted July 14, 2011 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/241917-accessing-and-processing-3rd-level-json-array-through-php/#findComment-1242541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.