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);
}
}
Related
Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}
I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}
I am trying to access data inside a php object. I am confused about what the exact stynax is. Here is the output of the object:
stdClass Object ( [vid] => 10 [uid] => 1 [title] => Beachhead Don [log] =>
[status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] =>
10 [type] => product [language] => und [created] => 1357668665 [changed] => 1358198386
[tnid] => 0 [translate] => 0 [revision_timestamp] => 1358198386 [revision_uid] => 1
[model] => B 3000 [list_price] => 0.00000 [cost] => 0.00000 [sell_price] => 1.00000
[weight] => 0 [weight_units] => lb [length] => 0 [width] => 0 [height] => 0
[length_units] => in [pkg_qty] => 1 [default_qty] => 1 [unique_hash] =>
2eec0fcc8483f3a3541870fb24223296 [ordering] => 0 [shippable] => 0 [price] => 1.00000
[body] => Array ( [und] => Array ( [0] => Array ( [value] => Description info
))))))))))))))))))))))))))))))))))))))0 [summary] => [format] => filtered_html
[safe_value] =>Description info ))))))))))))))))))))))))))))))))))))))0[safe_summary]
=> ) ) ) [uc_product_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 11
[alt] => [title] => [width] => 90 [height] => 120 [uid] => 1 [filename] =>
beachhead_don.jpg [uri] => public://beachhead_don.jpg [filemime] => image/jpeg
[filesize] => 14342 [status] => 1 [timestamp] => 1357668665 [rdf_mapping] => Array ( )
) ) ) [field_author] => Array ( [und] => Array ( [0] => Array ( [value] => John B.
Romesier [format] => [safe_value] => John B. Romesier ) ) ) [field_publisher] => Array
( [und] => Array ( [0] => Array ( [value] => Unknown [format] => [safe_value] =>
Unknown ) ) ) [field_release_date] => Array ( [und] => Array ( [0] => Array ( [value]
=> Mar 2012 [format] => [safe_value] => Mar 2012 ) ) ) [field_number_of_pages] => Array
( [und] => Array ( [0] => Array ( [value] => 123 ) ) ) [field_catagory] => Array (
[und] => Array ( [0] => Array ( [tid] => 4 ) ) ) [field_book_type] => Array ( [und] => Array ( [0] => Array ( [tid] => 1 ) ) ) [field_isbn] => Array ( ) [field_about_the_author] => Array ( [und] => Array ( [0] => Array ( [value] => Beachhed Don authoer............................ [format] => [safe_value] => Beachhed Don authoer............................ ) ) ) [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] => 1357668665 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
I am trying to retrive the [value] of Description info ))))))))))))))))))))))))))))))))))))))0
I tried this
<?php
$node = menu_get_object(); //drupal code
if ( !empty($node) ) {
print $node ->body=>und=>0=>value;
}?>
and got an error about the '=' sign. What is the proper way to get my data?
[body] and [und] are arrays, so access them like this:
echo $node->body['und'][0]['value'];
It should be:
$node->body['und'][0]['value']
'Body' and 'und' are Arrays and need to be accessed with array syntax([]).
Accessing these values directly is sometimes frowned upon in drupal. You may want to look into: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_render/7
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
I am attempting to add another child array to the last child array.
Here is a sample of my array,
Array
(
[Auto-Trail] => Array
(
[name] => Auto-Trail
[children] => Array
(
[2001] => Array
(
[name] => 2001
[children] => Array
(
[Tracker] => Array
(
[name] => Tracker
[children] => Array
(
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
[EK] => Array
(
[name] => EK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
)
)
[Cheyenne] => Array
(
[name] => Cheyenne
[children] => Array
(
[630S] => Array
(
[name] => 630S
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[630] => Array
(
[name] => 630
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634] => Array
(
[name] => 634
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634U] => Array
(
[name] => 634U
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
This is the array I wish to add to every last element,
For example, I need to add the following to [CK][children][Fiat][children] (and every last children's children) - this array will be static, every last child will use the same array.
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
So ... ][CK][children][Fiat] would become the following,
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
[children] => Array
(
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
)
)
)
)
Please note, Not every last child is the same indentation level, it could be 10 more children indentations. It must use the last array within the array of each array.
Apologies if it's hard to understand, I'm struggling to word exactly the way I need it to work.
Thank you for your time and effort.
check out this solution (quick'n'dirty):
function injectArray(array $target, array $inject, $depth = 0){
$hasChildrenKey = array_key_exists('children', $target);
$hasChildren = $hasChildrenKey && !empty($target['children']);
if($depth % 2 == 0){
foreach($target as $k => $v)
if(is_array($v))
$target[$k] = injectArray($v, $inject, $depth+1);
}
elseif(!$hasChildren){
if(!$hasChildrenKey)
$target['children'] = array();
$target['children'] = array_merge_recursive($target['children'], $inject);
}
else if($hasChildrenKey){
$target['children'] = injectArray($target['children'], $inject, $depth+1);
}
return $target;
};
$testArray = array(
'Auto-Trail' => array('name' => 'asdf',
'children' => array(
'2001' => array('name' => '2001',
'children' => array(
'Tracker' => array('name' => 'Tracker',
'children' => array(
'CK' => array('name' => 'CK',
'children' => array(
'Fiat' => array('name' => 'Fiat')
)
)
)
)
)
)
)
)
);
$testInjectArray = array('BOF' => array('name' => 'Furniture'), 'CHA' => array('name' => 'Chassis'));
$result = injectArray($testArray, $testInjectArray);
print_r($result);