Created
March 19, 2012 08:06
-
-
Save ravyg/2102108 to your computer and use it in GitHub Desktop.
takes in simple array to generate hierarchical nested array #tree #graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function hierarchical_array_from_array($farray){ | |
foreach ($flatArray as $key=>$flat) { | |
$nodes = array(); | |
$tree = array(); | |
foreach ($flatArray as $key=>&$node) { | |
$node["Children"] = array(); | |
$id = $node["@id"]; | |
$parent_id = $node["@parentId"]; | |
$nodes[$id] =& $node; | |
if (array_key_exists($parent_id, $nodes)) { | |
$nodes[$parent_id]["Children"][] =& $node | |
} else { | |
$tree[] =& $node; | |
} | |
} | |
return $tree; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tree #graph