parent-child tree in select box - php

I am trying to create a tree inside a select box with an unlimited number of parent and children.
and my select box (html part) should be Access and I have given a diagram below.
I have a "categories" array which has two main arrays with an unlimited number of nodes (parent-child). The two main array names are [scope] => selectboxFirst and [scope] => selectboxsecond. There is a parentid which starts with 4000. I cannot hard code parentid in the code. The arrays come from a decoded json string.
I don't know how to loop through this array and show both arrays in a separate select box.
In the example array I have posted, there are two main arrays inside one array but they can also come in other arrays. I don't know how to display this in two select box as parent child.
All the [scope] => selectboxFirst categories will display in first select box and [scope] => selectboxsecond categories in second select box.
Array
(
[status] => Array
(
[message] => Good
[code] => 200
)
[categories] => Array
(
[0] => Array
(
[scope] => selectboxFirst
[categories] => Array
(
[0] => Array
(
[id] => 4001
[parentId] => 4000
[name] => Access
[children] => Array
(
[0] => Array
(
[id] => 4010
[parentId] => 4001
[name] => mine
[type] => Reference
)
[1] => Array
(
[id] => 4011
[parentId] => 4001
[name] => yours
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4002
[parentId] => 4000
[name] => Communication
[children] => Array
(
[0] => Array
(
[id] => 4015
[parentId] => 4002
[name] => Physician
[children] => Array
(
[0] => Array
(
[id] => 4016
[parentId] => 4015
[name] => Helps blsys Understand
[type] => Reference
)
[1] => Array
(
[id] => 4017
[parentId] => 4015
[name] => Listens
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4021
[parentId] => 4002
[name] => Clinical ps
[children] => Array
(
[0] => Array
(
[id] => 4022
[parentId] => 4021
[name] => yours ps/datasssss
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4024
[parentId] => 4002
[name] => etc
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4003
[parentId] => 4000
[name] => Office
[children] => Array
(
[0] => Array
(
[id] => 4026
[parentId] => 4003
[name] => Facilities/Environment
[children] => Array
(
[0] => Array
(
[id] => 4027
[parentId] => 4026
[name] => Noise
[type] => Reference
)
[1] => Array
(
[id] => 4028
[parentId] => 4026
[name] => Lighting
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4032
[parentId] => 4003
[name] => Office ps
[children] => Array
(
[0] => Array
(
[id] => 4033
[parentId] => 4032
[name] => mine ps
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4035
[parentId] => 4003
[name] => Billing
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4004
[parentId] => 4000
[name] => Outgo
[children] => Array
(
[0] => Array
(
[id] => 4037
[parentId] => 4004
[name] => Diagnosis
[type] => Reference
)
[1] => Array
(
[id] => 4038
[parentId] => 4004
[name] => Quality of Procedure
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4005
[parentId] => 4000
[name] => Loyalty
[children] => Array
(
[0] => Array
(
[id] => 4039
[parentId] => 4005
[name] => Likely to also
[type] => Reference
)
)
[type] => Reference
)
)
)
[1] => Array
(
[scope] => selectboxSecond
[categories] => Array
(
[0] => Array
(
[id] => 4251
[parentId] => 4250
[name] => Communication with datas
[children] => Array
(
[0] => Array
(
[id] => 4262
[parentId] => 4251
[name] => respect?
[type] => Reference
)
[1] => Array
(
[id] => 4263
[parentId] => 4251
[name] => you?
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4252
[parentId] => 4250
[name] => Communication with Doctors
[children] => Array
(
[0] => Array
(
[id] => 4266
[parentId] => 4252
[name] => you with courtesy
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4253
[parentId] => 4250
[name] => Responsiveness of data ps
[children] => Array
(
[0] => Array
(
[id] => 4269
[parentId] => 4253
[name] => During
[type] => Reference
)
[1] => Array
(
[id] => 4270
[parentId] => 4253
[name] => How
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4254
[parentId] => 4250
[name] => Pain Management
[children] => Array
(
[0] => Array
(
[id] => 4271
[parentId] => 4254
[name] => During
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4255
[parentId] => 4250
[name] => Communication about Medicines
[children] => Array
(
[0] => Array
(
[id] => 4274
[parentId] => 4255
[name] => During this data stay
[type] => Reference
)
[1] => Array
(
[id] => 4275
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
[2] => Array
(
[id] => 4276
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
)
[type] => Reference
)
[5] => Array
(
[id] => 4256
[parentId] => 4250
[name] => Discharge Information
[children] => Array
(
[0] => Array
(
[id] => 4277
[parentId] => 4256
[name] => During this data stay the data?
[type] => Reference
)
[1] => Array
(
[id] => 4278
[parentId] => 4256
[name] => After you left the data
[type] => Reference
)
[2] => Array
(
[id] => 4279
[parentId] => 4256
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[6] => Array
(
[id] => 4257
[parentId] => 4250
[name] => Care Transition
[children] => Array
(
[0] => Array
(
[id] => 4280
[parentId] => 4257
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[7] => Array
(
[id] => 4258
[parentId] => 4250
[name] => Cleanliness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4282
[parentId] => 4258
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[8] => Array
(
[id] => 4259
[parentId] => 4250
[name] => Quietness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4283
[parentId] => 4259
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[9] => Array
(
[id] => 4260
[parentId] => 4250
[name] => also the data
[children] => Array
(
[0] => Array
(
[id] => 4284
[parentId] => 4260
[name] => Would you?
[type] => Reference
)
)
[type] => Reference
)
[10] => Array
(
[id] => 4261
[parentId] => 4250
[name] => Overall Rating of data
[children] => Array
(
[0] => Array
(
[id] => 4285
[parentId] => 4261
[name] => Using any number
[type] => Reference
)
)
[type] => Reference
)
)
)
)
)
the output shoiuld be something like for eg: The ids should come inside selectbox along with option
Access
Electronics
--Keyboards
--Phones
----HTC DESIRE Z
----HTC ONE X
-------Camera
-------Battety
Food
--Fruits
----Apple
----Watermelon
--Vegetables
----Carrot
----Onion
**inside select box . for eg: <select><option value='4001'>Access</option></select>**
I have so far this much but it displays everything .Still not getting
$json = json_decode($string,TRUE);
//echo "<pre>";
//print_r($json);
recursive($json,$level=1);
function recursive($array, $level = 1){
foreach($array as $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
recursive($value, $level + 1);
} else{
//It is not an array, so print it out.
echo str_repeat("-", $level), $value, '<br>';
}
}
}

The following code will recurse through your data structure and create the list of options. I will leave the creation of the select tag to you, since it is very simple. I've used PHP_EOL to add line breaks after each option to make the HTML code easier to read.
foreach ($data['categories'] as $c) {
# scope is $c['scope']
echo "Starting " . $c['scope'] . PHP_EOL;
# calling the function on the categories data
recurse($c['categories']);
echo "Finished " . $c['scope'] . PHP_EOL;
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['id'] . '">'
. str_repeat("-", $level)
. $n['name']
. '</option>'
. PHP_EOL;
# if item['children'] is set, we have a nested data structure, so
# call recurse on it.
if (isset($n['children'])) {
# we have children: RECURSE!!
recurse( $n['children'], $level+1);
}
}
}
Output:
Starting selectboxFirst
<option value="4001">Access</option>
<option value="4010">-mine</option>
<option value="4011">-yours</option>
<option value="4002">Communication</option>
<option value="4015">-Physician</option>
<option value="4016">--Helps blsys Understand</option>
<option value="4017">--Listens</option>
<option value="4021">-Clinical ps</option>
<option value="4022">--yours ps/datasssss</option>
<option value="4024">-etc</option>
<option value="4003">Office</option>
<option value="4026">-Facilities/Environment</option>
<option value="4027">--Noise</option>
(etc.)
You can substitute the appropriate code for "Starting selectboxFirst/selectboxSecond".

Related

Passing two nodes in PHP multidimensional array and get the level difference between them

I have a multidimensional array in PHP which looks like the following code:
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
This is the hierarchical array in which I want to pass two node ID's something like this -> getLevelDifference(7826, 5891) and it should give 2 as result or a function in which I can get that level child array like -> getLevelChild(7826, 2) and it should return 5891 as the result or other childs of the same level ?

How to replace array value with array value [have two array]

I try this array with array_merge() not working.
I have array (array one);
Array (
[0] => mf_3
[1] => mf_2
[2] => mf_1
[3] => mf_7
[4] => mf_6
[5] => mf_4
)
And on array again (second array),
Array (
[0] => stdClass Object
(
[id] => 1
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => 2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => 3
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => 4
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => 6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => 7
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Now, how to replace second array [id] with value from array one.
I want the result like this;
Array (
[0] => stdClass Object
(
[id] => mf_3
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => mf_2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => mf_1
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => mf_7
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => mf_6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => mf_4
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Please help, thanks.
Make use of foreach like below
foreach($first_array as $index => $farray)
{
$second_array[$index]->id = $farray;
}
foreach ($first_array as $key => $value) {
$second_array[$key]->id = $value;
}

PHP get facebook posts

$data = file_get_contents("https://graph.facebook.com/246179452202174/posts?access_token=TOKEN");
$data = json_decode($data, true);
print_r(array_values($data));
That's my code. It's simple. I can see the data and when I visit the link, it gives me the same info. So that works perfect. Underneath there's some outputs from the api.
Array ( [0] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) )
When I try to echo $data or maybe $data[1] or something like it, it wont work. Also removing array_values gives me a output of "Array" or "ArrayArray".
So how may I print out the data from the api? It wont work, also; how may I fetch the image? I'd like to print the title of the post, and the image. As a link for a feed at my site.
More response code for link:
Array ( [data] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) ) [1] => Array ( [id] => 246179452202174_680011585485623 [from] => Array ( [name] => Yoyo xno [category] => Musician/Band [id] => 281466768681242 ) [to] => Array ( [data] => Array ( [0] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) ) ) [message] => GG - Spotify: https://open.spotify.com/track/7tT6XVWfjtpNF8enpFpZT4 [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => wall_post [created_time] => 2016-05-12T17:33:49+0000 [updated_time] => 2016-05-12T17:33:49+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) ) [paging] => Array ( [cursors] => Array ( [before] => MTk2NDU4NjkwNzE2MzI5 [after] => MTk2NDU4NjkwNzE2MzI5 ) ) ) ) [2] => Array ( [id] => 246179452202174_676411742512274 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => Breaking news! Changes in the team. We are sad to announce that Aurora will part ways with Lgb Female. We wish to thank her for the time she spent with us and we wish her all the best for the future. Here's what she says about this herself: "My time with LGB eSports has come to an end. I am extremely thankful for the opportunity to grow within this team, go to amazing places and meet amazing people. I have learnt so much from this experience and I will never forget all the adventures we've had together! I would like to use this moment of change to look back and sincerely thank all my team-mates for this learning experience, Per Lilliefelth for looking after us, our sponsor Intel for taking care of us and LGB eSports for their immense contribution to helping us grow as an all-female team in Counter-Strike. I am certain I will still remain close to the team and look forward to seeing them at many future events. As for myself I am currently a free agent and look forward to continuing my journey as a competitive CS:GO player for many more years to come. Regards, Aurora Lyngdal" [picture] => https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13174168_676411742512274_4595019159434717947_n.jpg?oh=dad6bffaf94a2eae60c578d58f7cf8fc&oe=57E7E3F3 [link] => https://www.facebook.com/LGBeSports/photos/a.252900201530099.1073741829.246179452202174/676411742512274/?type=3 [name] => Timeline Photos [icon] => https://www.facebook.com/images/icons/photo.gif [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => photo [status_type] => added_photos [object_id] => 676411742512274 [created_time] => 2016-05-04T15:08:52+0000 [updated_time] => 2016-05-11T20:30:20+0000 [shares] => Array ( [count] => 4 ) [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1756499731303402 [name] => Antony Giordans ) [1] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) [2] => Array ( [id] => 10205058377778051 [name] => Charmaine Anne D. Better ) [3] => Array ( [id] => 992479757539742 [name] => Yunus Emre Çeker ) [4] => Array ( [id] => 244636029237859 [name] => Roman Hardinger ) [5] => Array ( [id] => 1134092109982746 [name] => Khoa Nguyen ) [6] => Array ( [id] => 525969817592366 [name] => Joseph Turton ) [7] => Array ( [id] => 1152533898100318 [name] => Kristian Sørensen ) [8] => Array ( [id] => 1193252910715325 [name] => Kamil Grochu Groszek ) [9] => Array ( [id] => 1158759100821275 [name] => Ace Kjirkovski ) [10] => Array ( [id] => 10153477966360064 [name]
you must echo this variable
$data[0][0]['message']

Iterating recursively through an array

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'];
}
}
}
}
}

Read complex array from php

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);

Categories