Jump to content

Looping through an array setting child arrays in position


jkkenzie

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.