Convert flat array to the multi-dimentional?

I don't think there is a built-in function in PHP that does this I tried the following code, and it seems to work to prepare the nested array the way you describe: $nodes = array(); $tree = array(); foreach ($source as &$node) { $node"Children" = array(); $id = $node"Menu""id"; $parent_id = $node"Menu""parent_id"; $nodes$id =& $node; if (array_key_exists($parent_id, $nodes)) { $nodes$parent_id"Children" =& $node; } else { $tree =& $node; } } var_dump($tree) I wrote a similar algorithm in a PHP class I wrote for my presentation erarchical Models in SQL and PHP but I was using objects instead of plain arrays.

I don't think there is a built-in function in PHP that does this. I tried the following code, and it seems to work to prepare the nested array the way you describe: $nodes = array(); $tree = array(); foreach ($source as &$node) { $node"Children" = array(); $id = $node"Menu""id"; $parent_id = $node"Menu""parent_id"; $nodes$id =& $node; if (array_key_exists($parent_id, $nodes)) { $nodes$parent_id"Children" =& $node; } else { $tree =& $node; } } var_dump($tree); I wrote a similar algorithm in a PHP class I wrote for my presentation erarchical Models in SQL and PHP, but I was using objects instead of plain arrays.

You are fantastic! Thanks for working solution and sush fast answer! The source array is the result of database query.

– bancer Jul 16 '10 at 1:55 Note that this algorithm works only if parents appear up in the db result set before their children appear. – Bill Karwin Jul 16 '10 at 2:05.

There is no short function to achieve that. You will have to program that.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions