Twee pass foreach doet de truc. Dit zal alle kinderen recursief aan hun ouders koppelen.
$structure = array();
foreach( $array as $row ) { //add rows to array by id
$structure[ $row["id"] ] = $row + array( "children" => array() );
}
foreach( $structure as &$row ) { //link children to parents
if( ! is_null( $row["parent"] ) ) {
$structure[ $row["parent"] ]["children"][] =& $row;
}
}