Page 1 of 1

Recursive function .

Posted: Sun Oct 05, 2014 8:53 pm
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 ??? :( :(

Re: Recursive function .

Posted: Sun Oct 05, 2014 9:50 pm
by Celauran
'template', 'index.php', and 'login.php' don't have array indices specified, so PHP is using numeric indices by default.

Re: Recursive function .

Posted: Mon Oct 06, 2014 12:52 pm
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>';

Re: Recursive function .

Posted: Wed Oct 08, 2014 12:25 pm
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 . ?

Re: Recursive function .

Posted: Wed Oct 08, 2014 12:44 pm
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.

Re: Recursive function .

Posted: Wed Oct 08, 2014 1:00 pm
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 .

Re: Recursive function .

Posted: Wed Oct 08, 2014 1:21 pm
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.

Re: Recursive function .

Posted: Wed Oct 08, 2014 1:44 pm
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.

Re: Recursive function .

Posted: Fri Oct 10, 2014 7:33 am
by gautamz07
Thanks ! i'll have to go over this a couple of times till it really sinks in :D TY