У меня есть следующий массив. Он имеет родительский идентификатор, соответствующий идентификатору. Мне удается создать функцию для сортировки и превращения этого массива в массив деревьев. Моя проблема заключается в том, что иногда это не работает должным образом, если родительский объект находится после ребенка.
Итак, как бы я превратил массив, подобный приведенному ниже, в дерево, которое не нужно сортировать первым?
[0] => Array ( [menu] => [parent] => 0 [id] => 1 ) , [1] => Array ( [menu] => [parent] => [id] => 2 ) , [2] => Array ( [menu] => [parent] => 1 [id] => 3 ) , [3] => Array ( [menu] => [parent] => 1 [id] => 4 ) , [4] => Array ( [menu] => [parent] => 4 [id] => 5 )
У меня есть эта функция, которая работает неправильно:
function page_tree($rows) { if(!is_array($rows) || empty($rows) ){ return false; } // $rows = array(); //stores all the database rows that are to be converted into a tree $tree = array(); //stores the tree $tree_index = array(); //an array used to quickly find nodes in the tree $id_column = "id"; //The column that contains the id of each node $parent_column = "parent"; //The column that contains the id of each node's parent $text_column = "title"; //The column to display when printing the tree to html //build the tree - this will complete in a single pass if no parents are defined after children // vp(count($rows) );die(); // while(count($rows) > 0){ foreach($rows as $row_id => $row){ $row_id = $row['id']; if($row[$parent_column]){ if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){ unset($rows[$row_id]); } else{ if(array_key_exists($row[$parent_column], $tree_index)){ $parent = & $tree_index[$row[$parent_column]]; $parent['children'][$row_id] =$row; $parent['children'][$row_id]["children"] = array(); $tree_index[$row_id] = & $parent['children'][$row_id]; unset($rows[$row_id]); } } } else{ $tree[$row_id] = $row; $tree[$row_id]["children"] = array(); $tree_index[$row_id] = & $tree[$row_id]; unset($rows[$row_id]); } } // } return $tree; }
неfunction page_tree($rows) { if(!is_array($rows) || empty($rows) ){ return false; } // $rows = array(); //stores all the database rows that are to be converted into a tree $tree = array(); //stores the tree $tree_index = array(); //an array used to quickly find nodes in the tree $id_column = "id"; //The column that contains the id of each node $parent_column = "parent"; //The column that contains the id of each node's parent $text_column = "title"; //The column to display when printing the tree to html //build the tree - this will complete in a single pass if no parents are defined after children // vp(count($rows) );die(); // while(count($rows) > 0){ foreach($rows as $row_id => $row){ $row_id = $row['id']; if($row[$parent_column]){ if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){ unset($rows[$row_id]); } else{ if(array_key_exists($row[$parent_column], $tree_index)){ $parent = & $tree_index[$row[$parent_column]]; $parent['children'][$row_id] =$row; $parent['children'][$row_id]["children"] = array(); $tree_index[$row_id] = & $parent['children'][$row_id]; unset($rows[$row_id]); } } } else{ $tree[$row_id] = $row; $tree[$row_id]["children"] = array(); $tree_index[$row_id] = & $tree[$row_id]; unset($rows[$row_id]); } } // } return $tree; }
неfunction page_tree($rows) { if(!is_array($rows) || empty($rows) ){ return false; } // $rows = array(); //stores all the database rows that are to be converted into a tree $tree = array(); //stores the tree $tree_index = array(); //an array used to quickly find nodes in the tree $id_column = "id"; //The column that contains the id of each node $parent_column = "parent"; //The column that contains the id of each node's parent $text_column = "title"; //The column to display when printing the tree to html //build the tree - this will complete in a single pass if no parents are defined after children // vp(count($rows) );die(); // while(count($rows) > 0){ foreach($rows as $row_id => $row){ $row_id = $row['id']; if($row[$parent_column]){ if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){ unset($rows[$row_id]); } else{ if(array_key_exists($row[$parent_column], $tree_index)){ $parent = & $tree_index[$row[$parent_column]]; $parent['children'][$row_id] =$row; $parent['children'][$row_id]["children"] = array(); $tree_index[$row_id] = & $parent['children'][$row_id]; unset($rows[$row_id]); } } } else{ $tree[$row_id] = $row; $tree[$row_id]["children"] = array(); $tree_index[$row_id] = & $tree[$row_id]; unset($rows[$row_id]); } } // } return $tree; }
неfunction page_tree($rows) { if(!is_array($rows) || empty($rows) ){ return false; } // $rows = array(); //stores all the database rows that are to be converted into a tree $tree = array(); //stores the tree $tree_index = array(); //an array used to quickly find nodes in the tree $id_column = "id"; //The column that contains the id of each node $parent_column = "parent"; //The column that contains the id of each node's parent $text_column = "title"; //The column to display when printing the tree to html //build the tree - this will complete in a single pass if no parents are defined after children // vp(count($rows) );die(); // while(count($rows) > 0){ foreach($rows as $row_id => $row){ $row_id = $row['id']; if($row[$parent_column]){ if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){ unset($rows[$row_id]); } else{ if(array_key_exists($row[$parent_column], $tree_index)){ $parent = & $tree_index[$row[$parent_column]]; $parent['children'][$row_id] =$row; $parent['children'][$row_id]["children"] = array(); $tree_index[$row_id] = & $parent['children'][$row_id]; unset($rows[$row_id]); } } } else{ $tree[$row_id] = $row; $tree[$row_id]["children"] = array(); $tree_index[$row_id] = & $tree[$row_id]; unset($rows[$row_id]); } } // } return $tree; }
Обратите внимание: где родительский (пустой) (= '';) означает, что это корень.