0

Hi,

I wrote a piece of code for doing a recursive printing of all the leaf nodes.

But it is not moving beyond one or two levels.

Here is the code:

$root = array(
		"paa" => array (
			"adi1" => array (
				"cir1" => array (
							"aka",
							"ra", "vinodh","dkido"
							),
															
				"cir2" => array (
						  	"muta",
							"la"
							),
																
				"cir3" => array	(
							"ezut",
							"telAm"
							),
																
				"cir4" => array	(
							"ati"
							)
				),

			"adi2" => array (

				"cir1" => array (
							"paka",
							"vaV"
							),
												
				"cir2" => array (												
							"mutaR",
							"RE"
							),
												
				"cir3" => array	(
							"ula",
							"ku"
							)
				)				
		                )
            );

function traverse($ar)
	{	
	
	foreach($ar as $key=>$value)					
					
          {
					
		echo "inside loop of ".$key."<br/>";
					
		if(is_array($value))				
			{
			    return traverse($value);
			}
		
		else
						
			{
			     echo $key."==>".$value."<br/>";
			}
				
	   }
				
					
	}

traverse($root);

The output I get is:

inside loop of paa
inside loop of adi1
inside loop of cir1
inside loop of 0
0==>aka
inside loop of 1
1==>ra
inside loop of 2
2==>vinodh
inside loop of 3
3==>dkido

It does not seem to visit the other nodes.

Anything I missed here ?

V

Edited by virtualvinodh: n/a

3
Contributors
3
Replies
7
Views
7 Years
Discussion Span
Last Post by virtualvinodh
0

Hi,

I think the issue is with the return statement. Try removing it, and let the function naturally return instead.

R.

0

-virtual

This is a perfect use of the RecursiveArrayIterator in the PHP5 SPL. You are using PHP5 right?

<?php

$root = array(
		"paa" => array (
			"adi1" => array (
				"cir1" => array (
							"aka",
							"ra", "vinodh","dkido"
							),
															
				"cir2" => array (
						  	"muta",
							"la"
							),
																
				"cir3" => array	(
							"ezut",
							"telAm"
							),
																
				"cir4" => array	(
							"ati"
							)
				),

			"adi2" => array (

				"cir1" => array (
							"paka",
							"vaV"
							),
												
				"cir2" => array (												
							"mutaR",
							"RE"
							),
												
				"cir3" => array	(
							"ula",
							"ku"
							)
				)				
		                )
            );

$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($root), RecursiveIteratorIterator::SELF_FIRST);

foreach($rit as $key=>$value){
	if( $rit->hasChildren() === TRUE ){
		echo 'inside loop of '.$rit->key().'<br />';
	} else {
		echo $key.'==>'.$value.'<br />';
	}
}
inside loop of paa
inside loop of adi1
inside loop of cir1
0==>aka
1==>ra
2==>vinodh
3==>dkido
inside loop of cir2
0==>muta
1==>la
inside loop of cir3
0==>ezut
1==>telAm
inside loop of cir4
0==>ati
inside loop of adi2
inside loop of cir1
0==>paka
1==>vaV
inside loop of cir2
0==>mutaR
1==>RE
inside loop of cir3
0==>ula
1==>ku

First we create a new RecursiveIteratorIterator object and pass it an instance of RecursiveArrayIterator. We pass $root in this case, your array, to the RecursiveArrayIterator. $rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($root), RecursiveIteratorIterator::SELF_FIRST); The second parameter RecursiveIteratorIterator::SELF_FIRST is one of three possibilities. LEAVES_ONLY, SELF_FIRST, CHILD_FIRST.


  • LEAVES_ONLY will only return leaves and not the containers.
  • SELF_FIRST will list the containers first and then each of their proceeding children.
  • CHILD_FIRST will reverse the way the current results look. Processing the recursive array from the inside out.

We can use a simple foreach loop to loop over the entire recursive array now.
$rit->hasChildren() and $rit->key() are function calls made against the RecursiveArrayIterator object.

This looks like a lot to process, and it probably is. Don't hesitate to ask any questions and I'll do my best to answer them. Once you start working with the functionality the SPL provides you'll realize a lot of commonly troublesome areas have already been addressed.

0

Thanks a lot mschroeder !

This seems to be very neat solution.

But still I would like to know what was wrong with my recursion :-)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.