Recursive function .

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Recursive function .

Post by gautamz07 »

in a file recursive.php i have the following code :

Code: Select all


<?php 
	require('recursive-function.php');


	$tree = array(
			'core' =>  array('Iteration One'),
			'classes' => array('Iteration One , Iteration One , Iteration One'),
			'functions' => array('Iteration One , Iteration One, Iteration One' , 
						'Iteration One' => array('Iteration two' , 'Iteration two')),
			'template',
			'index.php',
			'login.php'
			 );
	
	echo treeOut($tree);
?>
and in a file function i have the following code :

Code: Select all

<?php

	function treeOut($tree){
		$markup = "";
		foreach ($tree as $branch => $twig) {
			$markup .= '<li>' . $branch . '</li>';
		}

		return '<ul>'. $markup . '</ul>';
	}

?>
now , when i run this code , i get the following output :

Code: Select all

core
classes
functions
0
1
2
why the 0,1,2 ??? :( :(
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Recursive function .

Post by Celauran »

'template', 'index.php', and 'login.php' don't have array indices specified, so PHP is using numeric indices by default.
borre
Forum Newbie
Posts: 10
Joined: Thu Oct 02, 2014 7:21 am

Re: Recursive function .

Post by borre »

I think it is because you are always showing $branch.

Would is solve anything if you do something like:

Code: Select all

$markup .= '<li>' . (key($branch) ? $twig : $branch) . '</li>';
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Recursive function .

Post by gautamz07 »

Celauran wrote:'template', 'index.php', and 'login.php' don't have array indices specified, so PHP is using numeric indices by default.
so @Celauran you mean when i say the below

Code: Select all

$tree as $branch => $twig
i am basically saying go inside $tree and check all the element that are of type array and if an element is found to not to be an array just print 0 and soo on for the rest of the elements that are not an array . ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Recursive function .

Post by Celauran »

No. When you iterate over $tree, you're asking for the key and the value (that's your "as $branch => $twig" bit). Your function disregards the values and creates a list of the array's keys. When you don't explicitly specify a key for a particular value, PHP uses numeric keys internally. For instance,

Code: Select all

$foo = ['bar'];
echo treeOut($foo);
will produce 0, whereas

Code: Select all

$foo = ['granola' => 'bar'];
echo treeOut($foo);
will produce granola.

Also, despite the title of this thread, there's no recursion going on here.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Recursive function .

Post by gautamz07 »

Thanks for the explanation , i was playing around the function a little bit and discovered the same eg .

given the following code :

Code: Select all

        $tree = array( 'template', 'index.php', 'login.php'  );
	
	echo treeOut($tree);
         
and the following code :

Code: Select all

function treeOut($tree){
		$markup = "";
		foreach ($tree as $branch => $twig) {
			$markup .= '<li>' . $branch . '</li>';   
		}
		return '<ul>'. $markup . '</ul>';
	}
the output i get is 0,1,2.

and when i change the code to the following

Code: Select all

function treeOut($tree){
		$markup = "";
		foreach ($tree as $branch => $twig) {
			$markup .= '<li>' . $twig . '</li>' ; // only the $twig bit is changed :D    
		}
		return '<ul>'. $markup . '</ul>';
	}
the output is
template.php
index.php
login.php

Well i kind of get it now (: , i'll come to the recursion part in a while :D thanks .
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Recursive function .

Post by gautamz07 »

coming to the recursion part now . given the following code :

Code: Select all

<?php 
	require('recursive-function.php');


	$tree = array(
			'core' =>  array('Iteration One'),
			'classes' => array('Iteration One , Iteration One , Iteration One'),
			'functions' => array('Iteration One , Iteration One, Iteration One' , 
						'Iteration One' => array('Iteration two'  , 'Iteration two' => array('Iteration Three' , 'Iteration Three' => array('Iteration four')))),
			'template',
			'index.php',
			'login.php'
			 );
	
	echo treeOut($tree);
?>

and the folloing code :

Code: Select all

<?php

	function treeOut($tree){
		$markup = "";
		foreach ($tree as $branch => $twig) {
			$markup .= '<li>' . ((is_array($twig)) ? $branch . treeOut($twig) : $twig) . '</li>'; 
		}
		return '<ul>'. $markup . '</ul>';
	} 
?>
Now , heres my question . whats happening in the second piece of code on the 4th line .
my interpretation is as follows :
i see that the is_array function is being used in collaboration with the ternary operator so it reads :
if $(twig) is an array call the function treeOut() again , i really fail to understand this part

Code: Select all

$branch . treeOut($twig)
else append the $(twig) to <li> :D :D

it can be helpful if that part can be explained .

Thanks.

gautam.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Recursive function .

Post by Celauran »

Sounds like you've got it. If the current item is an array, create its tree/list and append it to the output.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Recursive function .

Post by gautamz07 »

Thanks ! i'll have to go over this a couple of times till it really sinks in :D TY
Post Reply