I use jstree to store a tree structure using JSON. My example structure looks like this:
After using
$treeJSONdecoded = json_decode($treeJSON, true);
my JSON looks like this:
[0] => Array (
[id] => j1_1
[text] => Release1
[icon] => 1
[li_attr] => Array (
[id] => j1_1
)
[a_attr] => Array (
[href] => #
[id] => j1_1_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] => 1
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_3
[text] => List of features
[icon] => 1
[li_attr] => Array (
[id] => j1_3
)
[a_attr] => Array (
[href] => #
[id] => j1_3_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_9
[text] => feature1
[icon] => 1
[li_attr] => Array (
[id] => j1_9
)
[a_attr] => Array (
[href] => #
[id] => j1_9_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default
)
[1 => Array ( id] => j1_2
[text] => List of documents
[icon] => 1
[li_attr] => Array (
[id] => j1_2
)
[a_attr] => Array (
[href] => #
[id] => j1_2_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_5
[text] => document1
[icon] => 1
[li_attr] => Array (
[id] => j1_5
)
[a_attr] => Array (
[href] => #
[id] => j1_5_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default )
)
[type] => default
)
How do I iterate through the whole JSON to get 'text' values and have an array like:
{"Release1", "List of features", ... , "document1"}
assuming that I don't know how many levels there are. I tried something like
foreach($treeJSONdecoded as $val){
echo $val['text'];
}
just to see what I can fetch but it doesn't seem to work.
Thanks #Scuzzy, this got me what I wanted:
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($treeJSON, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
$newArray = [];
foreach ($jsonIterator as $key => $val) {
if(!is_array($val) && $key == 'text') {
array_push($newArray, $val);
}
}
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 say :
$input = Array
(
[2] => Array
(
[message] => 1 file(s) do not conform to the xxx naming standards.
[type] => warning
[headers] => Array
(
[0] => Array
(
[name] => Package
[property] => zip
)
[1] => Array
(
[name] => Name
[property] => name
)
)
[rows] => Array
(
[0] => Array
(
[zip] => abcd.zip
)
)
)
[111] => Array
(
[message] => Invalid it is
[type] => error
[headers] => Array
(
[0] => Array
(
[name] => ID
[property] => id
)
[1] => Array
(
[name] => Title
[property] => title
)
[2] => Array
(
[name] => Zip
[property] => zip
)
)
[rows] => Array
(
[0] => Array
(
[id] => abcd
[title] => title
[zip] => abcd
)
)
)
)
Then I calculate length as below :
$length = count($this->view->feedback);
$i= 0;
Now I am running a foreach loop as
foreach ($input as $operation)
{
if($operation['type'] == 'error')
{
$output = array_slice($operation, -$i, $length);
}
$i++;
}
The above array_slice() method outputs as below :
$output =
Array
(
[rows] => Array
(
[0] => Array
(
[id] => 214501
[title] => SRM- 3860(Res-2)
[zip] => SRM-3860(Test_2).zip
)
)
)
My DESIRED/REQUIRED output is :
$output =
Array
(
[111] => Array
(
[message] => Invalid it is
[type] => error
[headers] => Array
(
[0] => Array
(
[name] => ID
[property] => id
)
[1] => Array
(
[name] => Title
[property] => title
)
[2] => Array
(
[name] => Zip
[property] => zip
)
)
[rows] => Array
(
[0] => Array
(
[id] => abcd
[title] => title
[zip] => abcd
)
)
)
)
How can I achieve this ?
Is something wrong with my array_slice method ?
What about this :
$my_desired_output = [];
foreach ($input as $key => $operation)
{
if($operation['type'] == 'error')
{
$my_desired_output[$key] = $operation;
}
}
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
Below is the result of the array from which I want to grab only coordinates and store them in to the one dimensional array.
Array
(
[name] => jackson
[Status] => Array
(
[code] => 200
[request] => geocode
)
[Placemark] => Array
(
[0] => Array
(
[#attributes] => Array
(
[id] => p1
)
[address] => Jackson, MS, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MS
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Hinds
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 32.3741783
[south] => 32.2232735
[east] => -90.0567509
[west] => -90.3128697
)
)
)
[Point] => Array
(
[coordinates] => -90.1848103,32.2987573,0
)
)
[1] => Array
(
[#attributes] => Array
(
[id] => p2
)
[address] => Jackson, TN, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => TN
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Madison
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 35.7562880
[south] => 35.5402259
[east] => -88.7567579
[west] => -88.9204599
)
)
)
[Point] => Array
(
[coordinates] => -88.8139469,35.6145169,0
)
)
[2] => Array
(
[#attributes] => Array
(
[id] => p3
)
[address] => Jackson, WY, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => WY
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Teton
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 43.4912050
[south] => 43.4578330
[east] => -110.7377220
[west] => -110.8134730
)
)
)
[Point] => Array
(
[coordinates] => -110.7624282,43.4799291,0
)
)
[3] => Array
(
[#attributes] => Array
(
[id] => p4
)
[address] => Jackson, NJ, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => NJ
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Ocean
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 40.1723549
[south] => 39.9990330
[east] => -74.2415390
[west] => -74.4695430
)
)
)
[Point] => Array
(
[coordinates] => -74.3294444,40.1080556,0
)
)
[4] => Array
(
[#attributes] => Array
(
[id] => p5
)
[address] => Jackson, MI, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MI
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Jackson
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 42.2708699
[south] => 42.2036839
[east] => -84.3568169
[west] => -84.4345460
)
)
)
[Point] => Array
(
[coordinates] => -84.4013462,42.2458690,0
)
)
[5] => Array
(
[#attributes] => Array
(
[id] => p6
)
[address] => Jackson, WI, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => WI
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Washington
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 43.3387369
[south] => 43.3090429
[east] => -88.1422039
[west] => -88.1917450
)
)
)
[Point] => Array
(
[coordinates] => -88.1667599,43.3238919,0
)
)
[6] => Array
(
[#attributes] => Array
(
[id] => p7
)
[address] => Jackson, CA, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => CA
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Amador
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 38.3721550
[south] => 38.3302920
[east] => -120.7489930
[west] => -120.7981980
)
)
)
[Point] => Array
(
[coordinates] => -120.7741018,38.3488023,0
)
)
[7] => Array
(
[#attributes] => Array
(
[id] => p8
)
[address] => Jackson, GA, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => GA
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Butts
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 33.3114890
[south] => 33.2638330
[east] => -83.9354860
[west] => -84.0105969
)
)
)
[Point] => Array
(
[coordinates] => -83.9660209,33.2945651,0
)
)
[8] => Array
(
[#attributes] => Array
(
[id] => p9
)
[address] => Jackson, OH 45640, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => OH
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Jackson
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 39.0669040
[south] => 39.0062730
[east] => -82.5949490
[west] => -82.7069350
)
)
)
[Point] => Array
(
[coordinates] => -82.6365536,39.0520169,0
)
)
[9] => Array
(
[#attributes] => Array
(
[id] => p10
)
[address] => Jackson, MO, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MO
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Cape Girardeau
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 37.4139659
[south] => 37.3385089
[east] => -89.5972280
[west] => -89.7035499
)
)
)
[Point] => Array
(
[coordinates] => -89.6662063,37.3822732,0
)
)
)
)
//data for single result starts here
Array
(
[Response] => Array
(
[name] => 10121
[Status] => Array
(
[code] => 200
[request] => geocode
)
[Placemark] => Array
(
[#attributes] => Array
(
[id] => p1
)
[address] => Manhattan, NY 10121, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 5
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => NY
[DependentLocality] => Array
(
[DependentLocalityName] => Manhattan
[PostalCode] => Array
(
[PostalCodeNumber] => 10121
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 40.7528519
[south] => 40.7489381
[east] => -73.9917906
[west] => -73.9947563
)
)
)
[Point] => Array
(
[coordinates] => -73.9917906,40.7492821,0
)
)
)
)
//data for single result ends here
I tried the following code for desired results but no success
foreach ($array as $xm) {
foreach ($xm as $points=>$pointkey) {
foreach($pointkey as $cor=>$corkey) {
echo $cor["coordinates"];
}
}
}
it gives me warning "Warning: Invalid argument supplied for foreach() in foreach($pointkey as $cor=>$corkey)"
Try something like:
$coords = array();
foreach ($data['Placemark'] as $entry) { // where $data holds the complete array
$coords[] = $entry['Point']['coordinates'];
}
var_dump($coords);
For only one result you can access the coordinates directly, like:
var_dump($data['Response']['Placemark']['Point']['coordinates']);
I would do something like this:
$coords = array();
array_walk_recursive($input_array, 'get_coords');
function get_coords($item, $key)
{
$coord = ($key === 'coordinates') ? $item : '';
if(!empty($coord))
{
$coords[] = $coord;
}
}
Array
(
[0] => Array
(
[#attributes] => Array
(
[request_id] => 0
[district] =>
[county] => WILTS
[ptc_abs_code] => 58150004231
[house_no] => 232
[post_town] => WESTBURY
[match_status] => 1
[house_name] =>
[postcode] => BA133BN
[surname] =>
[street_2] =>
[street_1] => HIGH ST
)
)
)