jkkenzie Posted January 15, 2013 Share Posted January 15, 2013 I have this array: Array ( [978_P] => Array( [986_P] => Array( [text] => Kenya [url] => /www/kilele/pages/Kenya.htm) [987_P] => Array( [text] => Tanzania National Parks [url] => /www/kilele/pages/Tanzania_National_Parks.htm) [988_P] => Array( [text] => Uganda National Parks [url] => /www/kilele/pages/Uganda_National_Parks.htm) ) [986_P] => Array( [989_P] => Array( [text] => Child1 [url] => /www/kilele/pages/Child1.htm) ) [990_P] => Array( [text] => Child2 [url] => /www/kilele/pages/Child2.htm) ) ) [990_P] => Array( [991_P] => Array( [text] => GrandChild1 [url] => /www/kilele/pages/GrandChild1.htm) ) [992_P] => Array( [text] => GrandChild2 [url] => /www/kilele/pages/GrandChild2.htm) ) ) ) NOTE: [986_P] inside the [978_P] has children in the [986_P] array: Same as [990_P] inside the [986_P] has children to [990_P] which are grandchildren to [986_P]. I can loop through these arrays to get all the [text] and as follows: foreach ($this->chItems[$Lnkkey] as $Res => $ChkLynks) { //print_r($this->chItems[$Lnkkey]); foreach ($ChkLynks as $ResLynks) { echo '<li><a href="'.$ResLynks["url"].'">'.$ResLynks["text"].'</a></li>'; } } The issue is that i need to get the children under their parents and the grandchildren under their parents too. as follows: <ul> <li><a href="/www/kilele/pages/Kenya.htm "> Kenya </a> <ul> <li><a href="/www/kilele/pages/Child1.htm"> Child1</a></li> <li><a href="/www/kilele/pages/Child2.htm"> Child2 </a> <ul> <li><a href="/www/kilele/pages/GrandChild1.htm"> GrandChild1 </a></li> . . . </ul> </li> </ul> </li> <li><a href=""></a></li> . . . </ul> I tried is_object but could not get that working: Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/ Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 I don't see anything that links the grandchild elements to their parent elements. You should make this a multi-dimensional array, or store the parent/foreign key in the array. Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405912 Share on other sites More sharing options...
jkkenzie Posted January 15, 2013 Author Share Posted January 15, 2013 how about the [986_P] array having the same name with the one inside the [978_P] array ? Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405914 Share on other sites More sharing options...
cpd Posted January 15, 2013 Share Posted January 15, 2013 It looks like an index naming convention gone wrong to me. What's generating this array? Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405916 Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 how about the [986_P] array having the same name with the one inside the [978_P] array ? So then where are you stuck? You already had someone write the above code for you in the other thread, now apply what you should have learned from that to this. Or restructure the array. I've done a lot of work like this, lately I had to write a report that had 8 levels of country, region, state, city, district, etc etc. It was easiest to do a multi-dimensional array that had many levels. (That report was not only deep in levels, but long (rows) and wide(columns) so we actually utilized some ajax loading and only loaded one level at a time, but the initial version that only had 3 levels would have looked something like this: IE: $locations = array( 'Country 1'=>array( 'text'=>'text', 'url'=>'url here', 'states'=>array( 'state 1'=>array( 'text'=>'text', 'url'=>'url here', 'cities'=>array( //etc etc etc ) ), 'state 2'=>array( 'text'=>'text', 'url'=>'url here', 'cities'=>array( //etc etc etc ) ) ) ), 'Country 2'=>array( 'text'=>'text', 'url'=>'url here', 'states'=>array(//etc etc etc) ) ); Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405917 Share on other sites More sharing options...
jkkenzie Posted January 15, 2013 Author Share Posted January 15, 2013 Here is the full array: Array ( [1] => Array( [962_P] => Array( [981_P] => Array([text] => why Kilele[url] => /www/kilele/pages/why_Kilele.htm) [982_P] => Array([text] => About Kenya[url] => /www/kilele/pages/About_Kenya.htm) [983_P] => Array([text] => Unique value proposition[url] => /www/kilele/pages/Unique_value_proposition.htm) [984_P] => Array([text] => About Tanzania[url] => /www/kilele/pages/About_Tanzania.htm) [985_P] => Array([text] => About Uganda[url] => /www/kilele/pages/About_Uganda.htm) ) ) [3] => Array( [978_P] => Array( [986_P] => Array([text] => Kenya[url] => /www/kilele/pages/Kenya.htm) [987_P] => Array([text] => Tanzania National Parks[url] => /www/kilele/pages/Tanzania_National_Parks.htm) [988_P] => Array([text] => Uganda National Parks[url] => /www/kilele/pages/Uganda_National_Parks.htm) ) [986_P] => Array( [989_P] => Array([text] => Te4st[url] => /www/kilele/pages/Te4st.htm) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405920 Share on other sites More sharing options...
Barand Posted January 15, 2013 Share Posted January 15, 2013 What's generating this array? That's a question worthy of an answer. Why do you enjoy creating array structures that you are not able to process? Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405961 Share on other sites More sharing options...
Barand Posted January 15, 2013 Share Posted January 15, 2013 Array ( [978_P] => Array( [986_P] => Array( [text] => Kenya => /www/kilele/pages/Kenya.htm) [987_P] => Array( [text] => Tanzania National Parks => /www/kilele/pages/Tanzania_National_Parks.htm) [988_P] => Array( [text] => Uganda National Parks => /www/kilele/pages/Uganda_National_Parks.htm) ) [986_P] => Array( [989_P] => Array( [text] => Child1 => /www/kilele/pages/Child1.htm) ) [990_P] => Array( [text] => Child2 => /www/kilele/pages/Child2.htm) ) ) [990_P] => Array( [991_P] => Array( [text] => GrandChild1 => /www/kilele/pages/GrandChild1.htm) ) [992_P] => Array( [text] => GrandChild2 => /www/kilele/pages/GrandChild2.htm) ) ) ) I'm glad I wasted no effort on answering how to process this array structure to give you what you want. It would have been a complete waste of of my time now I have seen the structure you posted later as your "complete array" with its extra dimension. I can only assume that I would be wasting my time again working on that one so early in this thread. (You didn't give the full picture in your last post either until much later on in the thread.) Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1405971 Share on other sites More sharing options...
jkkenzie Posted January 16, 2013 Author Share Posted January 16, 2013 Am sorry, people, am changing a module, to allow a new arrangement of the menu items: I was trying to work with the module AS IT IS, not to give myself alot of work. and what i provided previously, was to try not to give you alot of work, i thought that would solve my problem, but i encountered a need for a Recursion Algorithm later. thank you for your feedback, i have decided to use a function instead, which am working on now. Quote Link to comment https://forums.phpfreaks.com/topic/273200-looping-through-an-array-setting-child-arrays-in-position/#findComment-1406027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.