The requirement is that the original array is as follows. The plug-in using chrome with phpview (two-dimensional array) needs to be displayed in the following tree structure after "reverse recursion. Where parent_id is the level id of the upper level (the top level is 0), for example, parent_id125's user level is 1 ,...
The requirement is as follows. The original array is as follows, using chrome with the phpview plug-in (two-dimensional array)
After "reverse recursion", the tree structure is displayed as follows.
Here, parent_id is the level id of the upper level (the top level is 0). For example, if parent_id = 125's user level is 1, then the id of the upper level is 125. Thank you.
Reply content:
The requirement is as follows. The original array is as follows, using chrome with the phpview plug-in (two-dimensional array)
After "reverse recursion", the tree structure is displayed as follows.
Here, parent_id is the level id of the upper level (the top level is 0). For example, if parent_id = 125's user level is 1, then the id of the upper level is 125. Thank you.
Sorry, the answer is too late. I made a little complicated and used an internal sorting:
// $ Formated_arr is your input object // sort usort ($ formated_arr, function ($ a, $ B) by level first) {$ al = intval ($ a ['level']); $ bl = intval ($ B ['level']); return ($ al> $ bl )? 1:-1;}); // because the above sorting is done, although this is recursive, only the function reformat_tree (& $ arrTmp, $ parent_id = 0) {$ ret = null; foreach ($ arrTmp as $ k => $ v) {if ($ v ['parent _ id'] = $ parent_id) {$ ret [$ v ['id'] = $ v; unset ($ arrTmp [$ k]); $ child = reformat_tree ($ arrTmp, $ v ['id']);! Is_null ($ child )? $ Ret [$ v ['id'] ['child '] = $ child: 1 ;}return $ ret ;} echo (json_encode (reformat_tree ($ formated_arr )));
Try me this:
function createMenuTree($data = array(), $pid = 0){ if (empty($data)){ return array(); } static $level = 1; $returnArray = array(); foreach ($data as $node){ if ($node['parent_id'] == $pid){ $returnArray[] = array( 'cat_id' => $node['cat_id'], 'cat_name' => $node['cat_name'], 'level' => $level, 'parent_id' => $node['parent_id'], 'show_in_nav' => $node['show_in_nav'], 'is_show' => $node['is_show'], 'sort_order' => $node['sort_order'] ); if (hasChild($node['cat_id'], $data)){ $level++; $returnArray = array_merge($returnArray, createMenuTree($data, $node['cat_id'])); $level--; } } } return $returnArray;}function hasChild($cid, $data){ $hasChild = false; foreach ($data as $node){ if ($node['parent_id'] == $cid){ $hasChild = true; break; } } return $hasChild;}
The field is not very similar to yours, but the idea is similar to what you want. You can modify it by yourself.
function recursive_tree($arr,$level = 0,$parent_id = 0){ $tmp = array(); foreach ($arr as $key => $value) { if($value['level'] == $level && $value['parent_id'] == $parent_id){ $arr[$key]['child'] = recursive_tree($arr,$value['level'] + 1,$value['id']); if(empty($arr[$key]['child'])){ unset($arr[$key]['child']); } $tmp[] = $arr[$key]; } } return $tmp;}$arr = array( array( 'id' => '125', 'level' => '0', 'user_id' => '1021', 'parent_id' => '0', ), array( 'id' => '189', 'level' => '1', 'user_id' => '1022', 'parent_id' => '125', ), array( 'id' => '425', 'level' => '2', 'user_id' => '4119', 'parent_id' => '189', ), array( 'id' => '385', 'level' => '3', 'user_id' => '3170', 'parent_id' => '425', ), array( 'id' => '782', 'level' => '3', 'user_id' => '5698', 'parent_id' => '425', ), array( 'id' => '688', 'level' => '1', 'user_id' => '7045', 'parent_id' => '125', ));echo json_encode(recursive_tree($arr,0,0));die;
Http://segmentfault.com/q/1010000004052822/a-1020000004055246
Referring to the first half of my answers to this question, I explained in detail the not complex algorithm required by the question subject, but the question changed from OC to PHP.