Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a recusiv function with a multi array 1

Status
Not open for further replies.

GrodanBoll

Programmer
Mar 6, 2002
45
0
0
ZA
Hi :)

I have filled an array with data containing a hierarchical structure.
[tt]
$testarray = array (
1 => array ( "id" => 100, "descr" => "etthundra", "parentId" => "root" ),
2 => array ( "id" => 200, "descr" => "tvåhundra", "parentId" => "root" ),
3 => array ( "id" => 300, "descr" => "trehundra", "parentId" => "root" ),
4 => array ( "id" => 400, "descr" => "fyrahundra", "parentId" => "root" ),
5 => array ( "id" => 210, "descr" => "tvåhundratio", "parentId" => "2" ),
6 => array ( "id" => 211, "descr" => "tvåhundraelva", "parentId" => "5" ),
7 => array ( "id" => 220, "descr" => "tvåhundratjugo", "parentId" => "2" ),
8 => array ( "id" => 500, "descr" => "femhundra", "parentId" => "root" ),
9 => array ( "id" => 212, "descr" => "tvåhundratolv", "parentId" => "5" ),
11 => array ( "id" => 110, "descr" => "etthundratio", "parentId" => "1" ),
10 => array ( "id" => 600, "descr" => "sexhundra", "parentId" => "root" )

);
[/tt]


I'd like to show the data in hierarchical order. I have managed to do so in the function below.
[tt]
function displayChildren ( $parent, $level, $array )
{
/*
The format of the array is as such:
Array
(
[0] => Array
(
[key01] => Value01
[key02] => Value02
[key03] => Value03
[key04] => Value04
)

[1] => Array
(
[key01] => Value01
[key02] => Value02
[key03] => Value03
[key04] => Value04
)

[2] => Array
...
);
*/

$arrTemp01 = array();
$keyId = "parentId";



// Retrive all children of $parent and put them in the temp arry $arrTemp01.

for ( $i = 0; $i < count ( $array ); $i++ ) // Loop trough every main array element.
{
// In every sub array, check för the right value in the right key.

if ( $array [$i][$keyId] == $parent )
{
$arrTemp01[$i] = $array [$i];

}

}



// Display each child, if there where any children that is.

foreach ( $arrTemp01 as $key => $value )
{
echo ( str_repeat ( "&nbsp;&nbsp;&nbsp;&nbsp;", $level ).$value['descr']." Current key: ".$key."<br />\n" );

// Call this function again 2 display this child's children.

displayChildren ( $key, ($level+1), $array);

}


}
[/tt]

I call the function with:
[tt]
displayChildren ( "root", 0, $testarray );
[/tt]

To teh questions.

1. The problem is that I don't see the subarray with the largest index number (no 11).
Why?

2. If I put a subarray with index number zero then the loop will continue for ever.
Why does it do that?

Thanks :)
Grodan
 
I don't know why your element #11 wasn't displaying. The version below does.

The reason why your script gets into an infinite loop is because your parentId values are strings and your outermost array indeces are integers. When PHP compares a string to an integer, it converts the string to an integer. In the case of your original version, "root" gets converted to integer 0. This makes the parentId references a loop.


Code:
<?php	
$testarray = array (
     1 => array ( 'id' => 100, 'descr' => 'etthundra',      'parentId' => -1 ),
     2 => array ( 'id' => 200, 'descr' => 'tvåhundra',      'parentId' => -1 ),
     3 => array ( 'id' => 300, 'descr' => 'trehundra',      'parentId' => -1 ),
     4 => array ( 'id' => 400, 'descr' => 'fyrahundra',     'parentId' => -1 ),
     5 => array ( 'id' => 210, 'descr' => 'tvåhundratio',   'parentId' => 2 ),
     6 => array ( 'id' => 211, 'descr' => 'tvåhundraelva',  'parentId' => 5 ),
     7 => array ( 'id' => 220, 'descr' => 'tvåhundratjugo', 'parentId' => 2 ),
     8 => array ( 'id' => 500, 'descr' => 'femhundra',      'parentId' => -1 ),
     9 => array ( 'id' => 212, 'descr' => 'tvåhundratolv',  'parentId' => 5 ),
    11 => array ( 'id' => 110, 'descr' => 'etthundratio',   'parentId' => 1 ), 
    10 => array ( 'id' => 600, 'descr' => 'sexhundra',      'parentId' => -1 ),
    0 => array ( 'id' => 112	, 'descr' => 'etthundratolv', 'parentId' => 1 )
);



function displayChildren ( $parent, $level, $dataarray)
{
	$keyId = 'parentId';

	foreach ($dataarray as $key => $element)
	{
    		if ($element[$keyId] == $parent)
    		{
    			print str_repeat ("\t", $level);
			print $key . ':' . $element['descr']. "\n";
    			displayChildren($key, $level + 1, $dataarray);
    		}
    	}
}

displayChildren ( -1, 0 , $testarray);
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks a bunch sleipnir214. =]

Ur function was much nicer and cleaner, i'll steal it if U don't mind. ;]

Regarding that the last element was not showing, I hade an coditional error in my 'for' loop ($i <= count()). =/

Thanks
Grodan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top