GrodanBoll
Programmer
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 ( " ", $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 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 ( " ", $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