1: Arrange data directly into a tree structure
The data structure and methods are as follows:
$items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => 'Jiangxi Province'), 2 => array('id' => 2, 'pid' => 0, 'name' => 'Heilongjiang Province'), 3 => array('id' => 3, 'pid' => 1, 'name' => 'Nanchang City'), 4 => array('id' => 4, 'pid' => 2, 'name' => 'Harbin City'), 5 => array('id' => 5, 'pid' => 2, 'name' => 'Jixi City'), 6 => array('id' => 6, 'pid' => 4, 'name' => 'Xiangfang District'), 7 => array('id' => 7, 'pid' => 4, 'name' => 'Nangang District'), 8 => array('id' => 8, 'pid' => 6, 'name' => 'Hexing Road'), 9 => array('id' => 9, 'pid' => 7, 'name' => 'Xidazhi Street'), 10 => array('id' => 10, 'pid' => 8, 'name' => 'Northeast Forestry University'), 11 => array('id' => 11, 'pid' => 9, 'name' => 'Harbin Institute of Technology'), 12 => array('id' => 12, 'pid' => 8, 'name' => 'Harbin Normal University'), 13 => array('id' => 13, 'pid' => 1, 'name' => 'Ganzhou City'), 14 => array('id' => 14, 'pid' => 13, 'name' => 'Gan County'), 15 => array('id' => 15, 'pid' => 13, 'name' => 'Yudu County'), 16 => array('id' => 16, 'pid' => 14, 'name' => 'Maodian Town'), 17 => array('id' => 17, 'pid' => 14, 'name' => 'Datian Township'), 18 => array('id' => 18, 'pid' => 16, 'name' => 'Yiyuan Village'), 19 => array('id' => 19, 'pid' => 16, 'name' => 'Shangba Village'), ); function tree($items){<!-- --> $tree = array(); //Initialize tree structure data foreach($items as $value){<!-- --> if(isset($items[$value['pid']])){<!-- --> //If the current parent data exists $items[$value['pid']]['son'][] = & amp;$items[$value['id']]; //Add son element data to the corresponding parent element. The added data is current data }else{<!-- --> //If the current parent data does not exist $tree[] = & amp;$items[$value['id']]; //Add the current data to the tree structure data } } return $tree; } function genTree($items) {<!-- --> foreach ($items as $item) $items[$item['pid']]['son'][$item['id']] = & amp;$items[$item['id']]; //Same as above, just the key The value is different return isset($items[0]['son']) ? $items[0]['son'] : array(); } print_r(tree($items)); print_r(genTree($items));
The return result is as follows:
Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => Jiangxi Province [son] => Array ( [0] => Array ( [id] => 3 [pid] => 1 [name] => Nanchang City ) [1] => Array ( [id] => 13 [pid] => 1 [name] => Ganzhou City [son] => Array ( [0] => Array ( [id] => 14 [pid] => 13 [name] => Gan County [son] => Array ( [0] => Array ( [id] => 16 [pid] => 14 [name] => Maodian Town [son] => Array ( [0] => Array ( [id] => 18 [pid] => 16 [name] => Yiyuan Village ) [1] => Array ( [id] => 19 [pid] => 16 [name] => Shangba Village ) ) ) [1] => Array ( [id] => 17 [pid] => 14 [name] => Datian Township ) ) ) [1] => Array ( [id] => 15 [pid] => 13 [name] => Yudu County ) ) ) ) ) [1] => Array ( [id] => 2 [pid] => 0 [name] => Heilongjiang Province [son] => Array ( [0] => Array ( [id] => 4 [pid] => 2 [name] => Harbin City [son] => Array ( [0] => Array ( [id] => 6 [pid] => 4 [name] => Xiangfang District [son] => Array ( [0] => Array ( [id] => 8 [pid] => 6 [name] => Hexing Road [son] => Array ( [0] => Array ( [id] => 10 [pid] => 8 [name] => Northeast Forestry University ) [1] => Array ( [id] => 12 [pid] => 8 [name] => Harbin Normal University ) ) ) ) ) [1] => Array ( [id] => 7 [pid] => 4 [name] => Nangang District [son] => Array ( [0] => Array ( [id] => 9 [pid] => 7 [name] => West Dazhi Street [son] => Array ( [0] => Array ( [id] => 11 [pid] => 9 [name] => Harbin Institute of Technology ) ) ) ) ) ) ) [1] => Array ( [id] => 5 [pid] => 2 [name] => Jixi City ) ) ) ) Array ( [1] => Array ( [id] => 1 [pid] => 0 [name] => Jiangxi Province [son] => Array ( [3] => Array ( [id] => 3 [pid] => 1 [name] => Nanchang City ) [13] => Array ( [id] => 13 [pid] => 1 [name] => Ganzhou City [son] => Array ( [14] => Array ( [id] => 14 [pid] => 13 [name] => Gan County [son] => Array ( [16] => Array ( [id] => 16 [pid] => 14 [name] => Maodian Town [son] => Array ( [18] => Array ( [id] => 18 [pid] => 16 [name] => Yiyuan Village ) [19] => Array ( [id] => 19 [pid] => 16 [name] => Shangba Village ) ) ) [17] => Array ( [id] => 17 [pid] => 14 [name] => Datian Township ) ) ) [15] => Array ( [id] => 15 [pid] => 13 [name] => Yudu County ) ) ) ) ) [2] => Array ( [id] => 2 [pid] => 0 [name] => Heilongjiang Province [son] => Array ( [4] => Array ( [id] => 4 [pid] => 2 [name] => Harbin City [son] => Array ( [6] => Array ( [id] => 6 [pid] => 4 [name] => Xiangfang District [son] => Array ( [8] => Array ( [id] => 8 [pid] => 6 [name] => Hexing Road [son] => Array ( [10] => Array ( [id] => 10 [pid] => 8 [name] => Northeast Forestry University ) [12] => Array ( [id] => 12 [pid] => 8 [name] => Harbin Normal University ) ) ) ) ) [7] => Array ( [id] => 7 [pid] => 4 [name] => Nangang District [son] => Array ( [9] => Array ( [id] => 9 [pid] => 7 [name] => West Dazhi Street [son] => Array ( [11] => Array ( [id] => 11 [pid] => 9 [name] => Harbin Institute of Technology ) ) ) ) ) ) ) [5] => Array ( [id] => 5 [pid] => 2 [name] => Jixi City ) ) ) )
Reference: https://www.cnblogs.com/zzg521/p/6761576.html
Two: Find all children under the parent ID:
Test Data:
$arrCate = array( //Array to be sorted array('id' => 1, 'name' => 'Top column one', 'parent_id' => 0), array('id' => 2, 'name' => 'Top column two', 'parent_id' => 0), array('id' => 3, 'name' => 'Column 3', 'parent_id' => 1), array('id' => 4, 'name' => 'Column 4', 'parent_id' => 3), array('id' => 5, 'name' => 'Column 5', 'parent_id' => 4), array('id' => 6, 'name' => 'Column 6', 'parent_id' => 2), array('id' => 7, 'name' => 'Column 7', 'parent_id' => 6), array('id' => 8, 'name' => 'Column 8', 'parent_id' => 6), array('id' => 9, 'name' => 'column nine', 'parent_id' => 7), );
Get all children of the corresponding parent and return flat structure data
/** * Recursive infinite-level classification to obtain all children under any node * @param array $arrCate array to be sorted * @param int $parent_id parent node * @param int $level number of levels * @return array $arrTree sorted array (flattened data) */ function getTree($arrCate, $parent_id = 0, $level = 0){<!-- --> static $arrTree = []; //The variable value is still saved after the static function is executed if(empty($arrCate)) return []; $level + + ; foreach($arrCate as $key => $value){<!-- --> if($value['parent_id'] == $parent_id){<!-- --> $value['level'] = $level; $arrTree[] = $value; unset($arrCate[$key]); //Unregister the current node data to reduce useless traversal getTree($arrCate, $value['id'], $level); //Recursive call } } return $arrTree; } print_r(getTree($arrCate)); //The output results are as follows: Array ( [0] => Array ( [id] => 1 [name] => Top column one [parent_id] => 0 [level] => 1 ) [1] => Array ( [id] => 3 [name] => Column 3 [parent_id] => 1 [level] => 2 ) [2] => Array ( [id] => 4 [name] => Column 4 [parent_id] => 3 [level] => 3 ) [3] => Array ( [id] => 5 [name] => Column 5 [parent_id] => 4 [level] => 4 ) [4] => Array ( [id] => 2 [name] => Top column two [parent_id] => 0 [level] => 1 ) [5] => Array ( [id] => 6 [name] => Column 6 [parent_id] => 2 [level] => 2 ) [6] => Array ( [id] => 7 [name] => Column 7 [parent_id] => 6 [level] => 3 ) [7] => Array ( [id] => 9 [name] => Column 9 [parent_id] => 7 [level] => 4 ) [8] => Array ( [id] => 8 [name] => Column 8 [parent_id] => 6 [level] => 3 ) )
Get all children of the corresponding parent and return tree structure data
/** * Convert a two-dimensional array set into a Tree * @param $list data set * @param string $pk primary key * @param string $pid parent id * @param string $child child data key * @param int $root specifies the top-level id * @return array $tree processed data (tree structure) */ function listTree($list, $pk = 'id', $pid = 'parent_id', $child = 'children', $root = 0){<!-- --> $tree = []; if(is_array($list)){<!-- --> // Create an array reference based on the primary key $refer = array(); foreach($list as $key => $data){<!-- --> $refer[$data[$pk]] = & amp;$list[$key]; } foreach($list as $key => $data){<!-- --> // Determine whether parent exists $parentId = $data[$pid]; if($root == $parentId){<!-- --> $tree[] = & amp;$list[$key]; }else{<!-- --> if(isset($refer[$parentId])){<!-- --> $parent = & amp;$refer[$parentId]; $parent[$child][] = & amp;$list[$key]; } } } } return $tree; } print_r(listTree($arrCate)); //The output results are as follows: Array ( [0] => Array ( [id] => 1 [name] => Top column one [parent_id] => 0 [children] => Array ( [0] => Array ( [id] => 3 [name] => Column 3 [parent_id] => 1 [children] => Array ( [0] => Array ( [id] => 4 [name] => Column 4 [parent_id] => 3 [children] => Array ( [0] => Array ( [id] => 5 [name] => Column 5 [parent_id] => 4 ) ) ) ) ) ) ) [1] => Array ( [id] => 2 [name] => Top column two [parent_id] => 0 [children] => Array ( [0] => Array ( [id] => 6 [name] => Column 6 [parent_id] => 2 [children] => Array ( [0] => Array ( [id] => 7 [name] => Column 7 [parent_id] => 6 [children] => Array ( [0] => Array ( [id] => 9 [name] => Column 9 [parent_id] => 7 ) ) ) [1] => Array ( [id] => 8 [name] => Column 8 [parent_id] => 6 ) ) ) ) ) )
Three: Find all parent data based on child ID and return tree structure data
Test Data:
$arrCate = array( //Array to be sorted array('id' => 1, 'name' => 'Top column one', 'parent_id' => 0), array('id' => 2, 'name' => 'Top column two', 'parent_id' => 0), array('id' => 3, 'name' => 'Column 3', 'parent_id' => 1), array('id' => 4, 'name' => 'Column 4', 'parent_id' => 3), array('id' => 5, 'name' => 'Column 5', 'parent_id' => 4), array('id' => 6, 'name' => 'Column 6', 'parent_id' => 2), array('id' => 7, 'name' => 'Column 7', 'parent_id' => 6), array('id' => 8, 'name' => 'Column 8', 'parent_id' => 6), array('id' => 9, 'name' => 'column nine', 'parent_id' => 7), );
Find all parent data (flattened structure):
/** * Recursive infinite classification to obtain all parents of any node * @param array $arrCate array to be sorted * @param int $child_id child node * @return array $children sorted array (flattened data) */ function getparent($arrCate, $child_id = 9){<!-- --> static $children = []; foreach($arrCate as $key => $value){<!-- --> if($value['id'] == $child_id){<!-- --> $children[$value['id']] = $value; getparent($arrCate,$value['parent_id']); } } return $children; } print_r(getparent($arrCate)); //output example Array ( [9] => Array ( [id] => 9 [name] => Column 9 [parent_id] => 7 ) [7] => Array ( [id] => 7 [name] => Column 7 [parent_id] => 6 ) [6] => Array ( [id] => 6 [name] => Column 6 [parent_id] => 2 ) [2] => Array ( [id] => 2 [name] => Top column two [parent_id] => 0 ) )
Find all parent data (tree structure):
/** * Recursive infinite classification, obtain all parents of any node * @param array $arrCate array to be sorted * @param int $child_id child node * @return array $children sorted array */ function getparents($arrCate, $child_id = 9){<!-- --> static $children = []; foreach($arrCate as $key => $value){<!-- --> if($value['id'] == $child_id){<!-- --> $children[$value['id']] = $value; getparents($arrCate,$value['parent_id']); } } foreach($children as $k => $v){<!-- --> if($v['parent_id'] != 0){<!-- --> $children[$v['parent_id']]['children'] = $v; //Splice the children into the parent's children } if($v['parent_id'] == 0){<!-- --> $top_id = $v['id']; //Top node id } \t\t } \t return $children[$top_id]; //Return the tree structure data from the top to the specified child node \t } print_r(getparents($arrCate)); //Example output: Array ( [id] => 2 [name] => Top column two [parent_id] => 0 [children] => Array ( [id] => 6 [name] => Column 6 [parent_id] => 2 [children] => Array ( [id] => 7 [name] => Column 7 [parent_id] => 6 [children] => Array ( [id] => 9 [name] => Column 9 [parent_id] => 7 ) ) ) )
Reference: https://www.cnblogs.com/-mrl/p/13803740.html