Jump to content

Problem with recursive function


TeNDoLLA

Recommended Posts

Hello,

So I have an array with some test data and I have created a recursive function (actually two different versions) that should print out a nested list based on the parent - child relations within the array data. Below is the test array and the two functions I have created to print out the nested lists. For some reason it only prints out data with parent = 0 from the array. Anyone can point me out what I am doing wrong here?

 

test data array:

$test = array(	array('id' => 1, 'parent' =>0, 'name' => 'test1'),
			array('id' => 2, 'parent' =>0, 'name' => 'test2'),
			array('id' => 3, 'parent' =>1, 'name' => 'test3'),
			array('id' => 4, 'parent' =>1, 'name' => 'test4'),
			array('id' => 5, 'parent' =>1, 'name' => 'test5'),
			array('id' => 6, 'parent' =>0, 'name' => 'test6'),
			array('id' => 7, 'parent' =>0, 'name' => 'test7'),
			array('id' => 8, 'parent' =>0, 'name' => 'test8'),
			array('id' => 9, 'parent' =>0, 'name' => 'test9'),
			array('id' => 10, 'parent' =>0, 'name' => 'test10'),
			array('id' => 11, 'parent' =>10, 'name' => 'test11'),
			array('id' => 12, 'parent' =>10, 'name' => 'test12'),
			array('id' => 13, 'parent' =>10, 'name' => 'test13'),
			array('id' => 14, 'parent' =>13, 'name' => 'test14'),
			array('id' => 15, 'parent' =>13, 'name' => 'test15'),
			array('id' => 16, 'parent' =>0, 'name' => 'test16'),
			array('id' => 17, 'parent' =>0, 'name' => 'test17'),
			array('id' => 18, 'parent' =>0, 'name' => 'test18'),
			array('id' => 19, 'parent' =>0, 'name' => 'test19'),
			array('id' => 20, 'parent' =>0, 'name' => 'test20'),
		);

 

test function 1:

function createList($array, $parent = 0, $output = '')
{
$children = array();
foreach ($array as $key => $subArray)
{
	if ($subArray['parent'] == $parent)
	{
		$children[] = $subArray; 
	}
}
if (!empty($children))
{
	$output .= '<ul>';
	foreach ($children as $key => $subArray)
	{
		$output .= '<li>'. $subArray['name'] . createList($subArray, $subArray['id']) .'</li>';
	}
	$output .= '</ul>';
}
return $output;
}

 

test function 2:

function createList2($array, $parent = 0, $depth = 0, $output = '')
{
foreach ($array as $k => $subArray)
{
	if ($subArray['parent'] === $parent)
	{
		if ($depth == 0)
		{
			// Print out parent and call function again
			$output .= '<ul><li>'. $subArray['name'] . '</li>' . createList2($subArray, $subArray['id'], $depth+1) .'</ul>';
		}
		else
		{
			// Print children and call function again
			$output .= '<ul><li>'. $subArray['name'] . '</li>' . createList2($subArray, $subArray['id'], $depth+1) . '</ul>';
		}
	}
}
return $output;
}

 

using them:

echo createList($test)
echo createList2($test)

Link to comment
https://forums.phpfreaks.com/topic/243911-problem-with-recursive-function/
Share on other sites

Nevermind, I figured it out by modifying the function 1 a littlebit. Heres working code:

 

function createList($array, $parent = 0, $output = '')
{
$children = array();
foreach ($array as $key => $subArray)
{
	if ($subArray['parent'] === $parent)
	{
		$children[] = $subArray; 
	}
}

if (!empty($children))
{
	$output .= '<ul>';
	foreach ($children as $key => $subArray)
	{
		$output .= '<li>'. $subArray['name'] . createList($array, $subArray['id']) .'</li>';
	}
	$output .= '</ul>';
}
return $output;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.