Yesterday I asked this question: How to create this JSON in PHP?
This works perfectly and now I have to parse the reply from the server - he gives my an array like this example below. How can I run through this and get things like startTime and the following name?
And why does
<?php $json = file_get_contents("URL");
$data = json_decode($json);
$time1 = $data[0][1]['startTime'];
?>
not work? Please help me to understand this and please explain me how I can get to an solution next time when I have an array or JSON to parse and what you do when you have to parse things like this.
And is there any tool available, where you post the code and can click on the requested item and it shows you the code how to get this?
Array from server:
Array (
[jsonrpc] => 2.0
[id] => 6d41c7a9e35e441c4e9b66e267523927
[result] => Array (
[0] => Array (
[id] => 403230
[date] => 20190923
[startTime] => 1240
[endTime] => 1330
[kl] => Array (
[0] => Array (
[name] => Radiodata
)
)
[te] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[su] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[ro] => Array (
[0] => Array (
[id] => 31
)
)
[activityType] => Music-Only
)
[1] => Array (
[id] => 403231
[date] => 20190923
[startTime] => 1335
[endTime] => 1425
[kl] => Array (
[0] => Array (
[name] => Radiodata
)
)
[te] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[su] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[ro] => Array (
[0] => Array (
[id] => 31
)
)
[activityType] => Music-Only
)
[2] => Array (
[id] => 407790
[date] => 20190923
[startTime] => 1520
[endTime] => 1610
[kl] => Array (
[0] => Array (
[name] => Radiodata
)
)
[te] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[su] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[ro] => Array (
[0] => Array (
[id] => 24
)
)
[activityType] => Music-Only
)
[3] => Array (
[id] => 407791
[date] => 20190923
[startTime] => 1610
[endTime] => 1700
[kl] => Array (
[0] => Array (
[name] => Radiodata
)
)
[te] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[su] => Array (
[0] => Array (
[name] => MUS
[longname] => Music
)
)
[ro] => Array (
[0] => Array (
[id] => 24
)
)
[activityType] => Radiodata
)
)
Basically you parse the json with wrong key. Check this code which will help you
<?php
$json = file_get_contents("URL");
$data = json_decode($json,true);
foreach($data['result'] as $resData)
{
$startTime=$resData['startTime'];
$endtime=$resData['endTime'];
echo($startTime);
foreach($resData['kl'] as $klData)
{
$klname=$klData['name'];
echo($klname);
echo '<br>';
}
foreach($resData['te'] as $teData)
{
$tename=$teData['name'];
$echo($tename);
echo '<br>';
}
}
?>
Output:
Radiodata
MUS
Radiodata
MUS
Radiodata
MUS
Radiodata
MUS
Related
I have following url.
$url="https://graph.facebook.com/v2.8/page_id?fields=albums%7Bphotos.fields(source)%7D&access_token=access_token";
and getting data like this.
$ch=curl_init();
CURL_SETOPT($ch,CURLOPT_URL,$url);
CURL_SETOPT($ch,CURLOPT_RETURNTRANSFER, 1);
$json=json_decode(curl_exec($ch),true);
when I print the data it looks like below.
Array
(
[albums] => Array
(
[data] => Array
(
[0] => Array
(
[photos] => Array
(
[data] => Array
(
[0] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/15622392_1822020618075402_5390943482972338844_n.png?oh=e51cddbae2c4f9ca6926487207349674&oe=58F32530
[id] => 1822020618075402
)
[1] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/14695531_1788596578084473_7400591108780706934_n.jpg?oh=08e6f8301fd574edab84724005b5dcbd&oe=58DBBEB5
[id] => 1788596578084473
)
[2] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/14729405_1787260094884788_470106074060850845_n.jpg?oh=a9cae794a571078709ddede6a0a25fb7&oe=58ECA3E5
[id] => 1787260094884788
)
[3] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/14088463_1761208347489963_7246779259418379868_n.jpg?oh=3b86b61bd6eed58068d1ff55776fa5de&oe=58F47DEA
[id] => 1761208347489963
)
[4] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t31.0-8/s720x720/14125030_1758545434422921_4678448833207945291_o.jpg?oh=0b7f1455568f2ea36c80824c4531d820&oe=58FA25E0
[id] => 1758545434422921
)
[5] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/14067692_1755506721393459_3688009157574505621_n.jpg?oh=3fdf992a10562e7a62222424bbd7d284&oe=58DA3F4A
[id] => 1755506721393459
)
[6] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/13882196_1747456648865133_5440996625655127273_n.jpg?oh=b0f40cfee6858cd45f62e55630f2ac74&oe=58EF172F
[id] => 1747456648865133
)
[7] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/13782279_1745152582428873_8951006239758371779_n.jpg?oh=d329a7d4dd75975302708a147cf3f0e9&oe=58F14A03
[id] => 1745152582428873
)
[8] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/13659216_1740388646238600_1705878934725863431_n.jpg?oh=1ed9492d4ae713587a96edf4f38893b1&oe=58DB8EE7
[id] => 1740388646238600
)
[9] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/10885434_1516104578667009_8707702519302744521_n.jpg?oh=515cce8abab01bcdca68f14388664ae5&oe=58E0AEEB
[id] => 1516104578667009
)
[10] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/1937451_1505417943069006_8760716891932748781_n.jpg?oh=7a2a2daef9b1be7d3c98b35748893ea6&oe=58DCA006
[id] => 1505417943069006
)
[11] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-0/p180x540/10696291_1494026260874841_6924391974289072931_n.jpg?oh=df5d849e389d80c69c82118066445aa1&oe=58E08143
[id] => 1494026260874841
)
[12] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/10171825_1493583504252450_5141485588957894327_n.jpg?oh=17b0b20487ebb34be29621c355dba628&oe=58F0739D
[id] => 1493583504252450
)
[13] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/1513735_1492709627673171_1477328081447762519_n.jpg?oh=3012a617a0d48c4c736fc1e1ee428b26&oe=58EEAE38
[id] => 1492709627673171
)
[14] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/1515003_1490905571186910_3127181473382908591_n.png?oh=83bcf6d52c83ecc05d40b478afe71cd9&oe=58F6D6B2
[id] => 1490905571186910
)
[15] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/970678_1488554404755360_7889648772961232584_n.jpg?oh=3447943aeb630d6cfee3961e9bf8a444&oe=58E08CFE
[id] => 1488554404755360
)
[16] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/10676246_1487619861515481_8080018536585231766_n.jpg?oh=c8e143320188e22170c1f28fa52fcfae&oe=58E6C546
[id] => 1487619861515481
)
[17] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/q82/s720x720/10689897_1486654284945372_4989681801007970605_n.jpg?oh=d67a02472ce4aff6c32d443377a21e9a&oe=58E73B16
[id] => 1486654284945372
)
[18] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t31.0-0/p180x540/10750146_1485256888418445_36234076640555733_o.jpg?oh=5e486c35cee8f4ecba8743948e6e4cb3&oe=58F9C0CE
[id] => 1485256888418445
)
[19] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/560161_1484719421805525_2089957304428778741_n.jpg?oh=099d53b6aacfaa10b7a119c48179c1c3&oe=58E21E6C
[id] => 1484719421805525
)
[20] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/1377050_1484034435207357_151327894676844014_n.jpg?oh=83eb8c6b25412b067b4a000fa5f97a99&oe=58F7DB51
[id] => 1484034435207357
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => MTgyMjAyMDYxODA3NTQwMgZDZD
[after] => MTQ4NDAzNDQzNTIwNzM1NwZDZD
)
)
)
[id] => 1484035021873965
)
[1] => Array
(
[photos] => Array
(
[data] => Array
(
[0] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/13782058_1742422396035225_5477589171813279082_n.png?oh=364dda35704379d15336ab1f9144659d&oe=58EA49E7
[id] => 1742422396035225
)
[1] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/10350506_1484031238541010_5203658133196952315_n.jpg?oh=26dbeba4df1f650e4acbea3a1aad5164&oe=58EC962B
[id] => 1484031238541010
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => MTc0MjQyMjM5NjAzNTIyNQZDZD
[after] => MTQ4NDAzMTIzODU0MTAxMAZDZD
)
)
)
[id] => 1484031228541011
)
[2] => Array
(
[photos] => Array
(
[data] => Array
(
[0] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/13669028_1742420099368788_169584588534858741_n.jpg?oh=2ceec7f3f1890dfff93dfd097d9da11b&oe=58E9A3B8
[id] => 1742420099368788
)
[1] => Array
(
[source] => https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/10687154_1481995178744616_1243763385874632903_n.jpg?oh=0ed79383008455b3326d174dd5935b4b&oe=58FBB705
[id] => 1481995178744616
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => MTc0MjQyMDA5OTM2ODc4OAZDZD
[after] => MTQ4MTk5NTE3ODc0NDYxNgZDZD
)
)
)
[id] => 1481995338744600
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => MTQ4NDAzNTAyMTg3Mzk2NQZDZD
[after] => MTQ4MTk5NTMzODc0NDYwMAZDZD
)
)
)
[id] => page_id
)
Now i want source from array and put this into an image tag. like below.
<img src="url_from the array for each picture"/>
I am trying like this, but not getting source.
foreach($json as $key){
echo $key['source'];
}
How can i get source in loop and display all images?
Change your json_encode line to this: $json=json_decode(curl_exec($ch));
Then access your info as such:
foreach($json->albums->data as $album){
foreach($album->photos->data as $photo){
echo '<img src="'.$photo->source.'"/>';
}
}
You need to loop the albums, and then the photos in these albums, something like:
$albums = $json["albums"];
foreach ($albums["data"] as $album) {
foreach($album["photos"]["data"] as $photo) {
echo $photo["source"];
}
}
This is a multi dimensional array.
Its returning all the albums and the photos within.
Use the following to loop through and display all the images..
// for each Album
foreach($json['albums']['data'] as $album)
{
// for each photos in current album
foreach($album['photos']['data'] as $photo )
{
echo '<img src="'. $photo['source'] .'"/>';
}
}
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)
I have an array as below:
Array
(
[0] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[1] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
[2] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
)
I would like to re-order the array by only the last datetime inside the sub array [batch]
the result should be like this:
Array
(
[0] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
[1] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[2] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
)
Can I do it with "array_multisort"? Please suggest.
Thank you very much!
Use usort which allows you to sort an array using a user-defined comparison function. (PHP Docs)
usort($array, function($a, $b) {
$lastTimestampA = end($a['batch']);
$lastTimestampB = end($b['batch']);
return strtotime($lastTimestampA['datetime']) > strtotime($lastTimestampB['datetime']);
})
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I've got a complex array variable called $data. I need to pull out one very buried value: [url] in [field_website]. Here's the raw print_r() output of the $data variable:
stdClass Object ( [node_title] => CMI2 [nid] => 3 [field_data_field_website_node_entity_type] => node [field_data_field_blog_node_entity_type] => node [field_data_field_rss_node_entity_type] => node [field_data_field_twitter_node_entity_type] => node [field_data_field_yammer_node_entity_type] => node [field_data_field_facebook_node_entity_type] => node [field_data_field_flickr_node_entity_type] => node [field_data_field_youtube_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 3 [uid] => 1 [title] => CMI2 [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 3 [type] => social_source [language] => und [created] => 1356040541 [changed] => 1356040541 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1356040541 [revision_uid] => 1 [field_website] => Array ( [und] => Array ( [0] => Array ( [url] => http://cmi2.yale.edu [title] => [attributes] => Array ( ) ) ) ) [field_blog] => Array ( ) [field_rss] => Array ( ) [field_twitter] => Array ( [und] => Array ( [0] => Array ( [url] => http://twitter.com/yalecmi2 [title] => [attributes] => Array ( ) ) ) ) [field_facebook] => Array ( ) [field_youtube] => Array ( ) [field_flickr] => Array ( ) [field_yammer] => Array ( ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1356040541 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; ) ) ) [field_field_website] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://cmi2.yale.edu [#access] => 1 ) [raw] => Array ( [url] => http://cmi2.yale.edu [title] => http://cmi2.yale.edu [attributes] => Array ( ) [display_url] => http://cmi2.yale.edu ) ) ) [field_field_blog] => Array ( ) [field_field_rss] => Array ( ) [field_field_twitter] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://twitter.com/yalecmi2 [#access] => 1 ) [raw] => Array ( [url] => http://twitter.com/yalecmi2 [title] => http://twitter.com/yalecmi2 [attributes] => Array ( ) [display_url] => http://twitter.com/yalecmi2 ) ) ) [field_field_yammer] => Array ( ) [field_field_facebook] => Array ( ) [field_field_flickr] => Array ( ) [field_field_youtube] => Array ( ) )
(sorry about that ugliness!)
How the heck do I pull out that [url] variable?! Ideally, I just want to assign that one value to another variable or just print it out.
If it's helpful, this is from a Drupal 7 view with the Views PHP module.
Thanks!
I recommend reading up about PHP arrays and objects since what you are trying to do is so trivial.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/sdo.sample.getset.php