Trying to get ID from multidimensional array PHP - php

I have an array, which has been created from getting the pages from user levels. When I use print_r to print out the array you can see that there are arrays nested within arrays.
I am trying to access the 'ID' and 'name' values, which exists in each nested array. I have tried using a foreach and for loop, but have had no luck. Any help would be greatly appreciated.
Array ( [0] => Array ( [0] => Array ( [ID] => 2487 [name] => Student Analytics [_more_] => /levels/1531756603/pages/2487 )
[1] => Array ( [ID] => 1048 [name] => Member Profile [_more_] => /levels/1531756603/pages/1048 )
[2] => Array ( [ID] => 864 [name] => i.4 Course Practicum – Your Live Campaign [_more_] => /levels/1531756603/pages/864 )
[3] => Array ( [ID] => 817 [name] => i.5 Course Expectations – Contact Us If Needed [_more_] => /levels/1531756603/pages/817 )
[4] => Array ( [ID] => 815 [name] => i.3 Course Assessments – Why There Are Level Assessments [_more_] => /levels/1531756603/pages/815 )
[5] => Array ( [ID] => 813 [name] => i.2 Course Structure – How to Navigate the Web Platform [_more_] => /levels/1531756603/pages/813 )
[6] => Array ( [ID] => 807 [name] => Introduction to the Course [_more_] => /levels/1531756603/pages/807 )
[7] => Array ( [ID] => 659 [name] => INTRO LEVEL ASSESSMENT [_more_] => /levels/1531756603/pages/659 )
[8] => Array ( [ID] => 378 [name] => How This Course Will be Taught [_more_] => /levels/1531756603/pages/378 )
[9] => Array ( [ID] => 376 [name] => SCourse [_more_] => /levels/1531756603/pages/376 )
[10] => Array ( [ID] => 372 [name] => Dashboard [_more_] => /levels/1531756603/pages/372 ) )
[1] => Array ( [0] => Array ( [ID] => 2372 [name] => Profile Assignment [_more_] => /levels/1531866216/pages/2372 )
[1] => Array ( [ID] => 2110 [name] => Instructions [_more_] => /levels/1531866216/pages/2110 )
[2] => Array ( [ID] => 1619 [name] => Practicum [_more_] => /levels/1531866216/pages/1619 )
[3] => Array ( [ID] => 1048 [name] => Member Profile [_more_] => /levels/1531866216/pages/1048 )
[4] => Array ( [ID] => 571 [name] => LEVEL 1 [_more_] => /levels/1531866216/pages/571 )
[5] => Array ( [ID] => 506 [name] => 2.A ASSIGNMENTS [_more_] => /levels/1531866216/pages/506 )
[6] => Array ( [ID] => 504 [name] => 2.7 SUMMARY AND REVIEW [_more_] => /levels/1531866216/pages/504 )
[7] => Array ( [ID] => 502 [name] => 2.6 Your Brand is the Balancing Force [_more_] => /levels/1531866216/pages/502 )
[8] => Array ( [ID] => 497 [name] => 2.5 Brand Trust [_more_] => /levels/1531866216/pages/497 )
[9] => Array ( [ID] => 494 [name] => 2.4 Brand Beliefs [_more_] => /levels/1531866216/pages/494 )
[10] => Array ( [ID] => 491 [name] => 2.3 Brand Promise [_more_] => /levels/1531866216/pages/491 )
[11] => Array ( [ID] => 487 [name] => 2.2 Brand Story [_more_] => /levels/1531866216/pages/487 )
[12] => Array ( [ID] => 484 [name] => 2.1 Branding [_more_] => /levels/1531866216/pages/484 )
[13] => Array ( [ID] => 478 [name] => CHAPTER 2 CREATING THE BRAND HUB: FOUR KEY BRAND ELEMENTS [_more_] => /levels/1531866216/pages/478 )
[14] => Array ( [ID] => 473 [name] => LEVEL 1 ASSESSMENT [_more_] => /levels/1531866216/pages/473 )
[15] => Array ( [ID] => 464 [name] => 2.A-1 Brand Strategy Assignment [_more_] => /levels/1531866216/pages/464 )
[16] => Array ( [ID] => 461 [name] => 2.A-4 Student Instructions [_more_] => /levels/1531866216/pages/461 )
[17] => Array ( [ID] => 459 [name] => 2.A-3 Project Submission Form [_more_] => /levels/1531866216/pages/459 )
[18] => Array ( [ID] => 448 [name] => 1.A PRACTICUM ASSIGNMENTS [_more_] => /levels/1531866216/pages/448 )
[19] => Array ( [ID] => 445 [name] => 1.A-2 Topical Brainstorm Assignment [_more_] => /levels/1531866216/pages/445 )
[20] => Array ( [ID] => 432 [name] => 1.5 SUMMARY AND REVIEW [_more_] => /levels/1531866216/pages/432 )
[21] => Array ( [ID] => 427 [name] => 1.4 A New Inbound Model [_more_] => /levels/1531866216/pages/427 )
[22] => Array ( [ID] => 423 [name] => 1.3 The New Social Era of Marketing [_more_] => /levels/1531866216/pages/423 )
[23] => Array ( [ID] => 410 [name] => 1.2 Traditional Outbound Marketing vs. Digital Inbound Marketing [_more_] => /levels/1531866216/pages/410 )
[24] => Array ( [ID] => 394 [name] => 1.1 Consumer Acquisition from the Digital Landscape [_more_] => /levels/1531866216/pages/394 )
[25] => Array ( [ID] => 392 [name] => CHAPTER 1 INTRODUCING [_more_] => /levels/1531866216/pages/392 )
[26] => Array ( [ID] => 318 [name] => Project Submission Thank You [_more_] => /levels/1531866216/pages/318 ) ) )

Loop through the array like so.
foreach($pages_array_name['0'] as $index => $sub_array) {
echo "index is " . $index . "\n<br/>";
echo "name is " . $sub_array['name'] . "\n<br/>";
echo "_more_ is " . $sub_array['_more_'] . "\n<br/>";
echo "\n<br/>"; // extra space
}

Related

Foreach Undefined Index

Am trying to loop through an array,with print_r I get the array below but am getting
"Notice: Undefined index: name"
error, when I ran my loop, dunno what am doing wrong?
Kindly help
Array
(
[0] => Array
(
[0] => Array
(
[ID] => 1
[name] => Paul
[email] => 32
)
[1] => Array
(
[ID] => 2
[name] => Allen
[email] => 25
)
[2] => Array
(
[ID] => 3
[name] => Teddy
[email] => 23
)
[3] => Array
(
[ID] => 4
[name] => 4
[email] => Mark
)
[4] => Array
(
[ID] => 5
[name] => Paul
[email] => 32
)
[5] => Array
(
[ID] => 6
[name] => Allen
[email] => 25
)
[6] => Array
(
[ID] => 7
[name] => Teddy
[email] => 23
)
[7] => Array
(
[ID] => 8
[name] => 4
[email] => Mark
)
[8] => Array
(
[ID] => 9
[name] => Paul
[email] => 32
)
[9] => Array
(
[ID] => 10
[name] => Allen
[email] => 25
)
[10] => Array
(
[ID] => 11
[name] => Teddy
[email] => 23
)
[11] => Array
(
[ID] => 12
[name] => 4
[email] => Mark
)
[12] => Array
(
[ID] => 13
[name] => Paul
[email] => 32
)
[13] => Array
(
[ID] => 14
[name] => Allen
[email] => 25
)
[14] => Array
(
[ID] => 15
[name] => Teddy
[email] => 23
)
[15] => Array
(
[ID] => 16
[name] => 4
[email] => Mark
)
[16] => Array
(
[ID] => 17
[name] => Paul
[email] => ibraq22#yahooo.com
)
[17] => Array
(
[ID] => 18
[name] => Opeyemi Adam
[email] => resfvr#yahoo.com
)
[18] => Array
(
[ID] => 19
[name] => Opeyemi Adam
[email] => info#ckdigital.net
)
[19] => Array
(
[ID] => 20
[name] => Paul
[email] => ibraq22#yahooo.com
)
)
)
My PHP Loop is below
foreach ($retr as $page) {
echo $page['name'];
//var_dump($page);
}
Regards
Your array is multidimentional, being a single element array containing a further 19 child elements.
If you just want to itterate the child elements, then run the loop over the top level elelemt, not the parent array:
foreach ($retr[0] as $page) {...}

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

Manipulate a Multi-Multi-Dimensional PHP Array into a nice Multi-Dimensional Array

I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!
In PHP I have this variable $design_values
If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.
In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1
In the application there can be any number of Designs from 0 up to any number.
Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.
These 24 Array keys are numbered 0 to 23.
Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.
I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.
So the name and value keys should make up a key => value. The New array should look more like this....
The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.
If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.
Desired New Array Format :
Array
(
[0] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
),
[1] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
)
)
Current Array Format :
Array
(
[0] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
)
[4] => Array
(
[name] => name
[value] => test
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:29:32
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-01 19:29:32
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] =>
)
[15] => Array
(
[name] => design_number
[value] =>
)
[16] => Array
(
[name] => overall_height
[value] =>
)
[17] => Array
(
[name] => overall_width
[value] =>
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
[1] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 86f21f44-4b21-1826-3592-542c59e4be66
)
[4] => Array
(
[name] => name
[value] => fdtgrfdhg
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:41:54
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-19 19:30:45
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] => design name
)
[15] => Array
(
[name] => design_number
[value] => 313
)
[16] => Array
(
[name] => overall_height
[value] => 22
)
[17] => Array
(
[name] => overall_width
[value] => 22
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
)
If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):
foreach($design_values as $key => $values) {
$result[$key] = array_combine(
array_column($values, 'name'),
array_column($values, 'value'));
}
Or possibly, and easier:
foreach($design_values as $key => $values) {
$result[$key] = array_column($values, 'value', 'name');
}
Our use the PHP Implementation of array_column
You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:
$newArray = array();
foreach($oldArray as $row){
$tmp = array();
foreach($row as $values){
$tmp[$values['name']] = $values['value'];
}
$newArray[] = $tmp;
}
print_r($newArray);

parent-child tree in select box

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".

CakePHP and Set::combine

I need to get the data for each language in it's field as an array.
Probably the best approach in CakePHP will be Set::combine but can't get it working.
I can do it manually with foreach but I don't think that will be the best way.
Here is the example:
Array
(
[Article] => Array
(
[id] => 131
[title] => TEST
)
[titleTranslation] => Array
(
[0] => Array
(
[id] => 62
[locale] => eng
[model] => Article
[foreign_key] => 131
[field] => title
[content] => TEST
)
[1] => Array
(
[id] => 63
[locale] => fre
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Salva
)
[2] => Array
(
[id] => 64
[locale] => rus
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Пвет
)
)
)
into this array:
Array
(
[Article] => Array
(
[id] => 131
[title] => Array
(
[eng] => TEST
[fre] => Salva
[rus] => Пвет
)
)
.... the rest is not important
)
Solved-----
$translatedData = Set::combine($this->data['titleTranslation'], '{n}.locale', '{n}.content', '{n}.field');
$this->data['Article'] = array_merge($this->data['Article'], $translatedData);

Categories