Create array tree of nth level form array list

荒凉一梦 提交于 2021-01-29 13:02:46

问题


I have only Nth number of level array return, this function return all children list

$arr = array(
array('id'=>100, 'parentid'=>0, 'name'=>'a'),


array('id'=>101, 'parentid'=>100, 'name'=>'a'),
  array('id'=>102, 'parentid'=>101, 'name'=>'a'),
  array('id'=>103, 'parentid'=>101, 'name'=>'a'),
);

$new = array();
foreach ($arr as $a){
    $new[$a['parentid']][] = $a;
}
$tree = createTree($new, array($arr[0]));
print_r($tree);

function createTree(&$list, $parent){
    $tree = array();
    foreach ($parent as $k=>$l){
        if(isset($list[$l['id']])){
            $l['children'] = createTree($list, $list[$l['id']]);
        }
        $tree[] = $l;
    } 
    return $tree;
}

this function return all data in array.

this function return cant return the particular level is complete then break and return values


回答1:


Try like this.

$array = array(
    array('id'=>100, 'parentid'=>0, 'name'=>'a'),
    array('id'=>101, 'parentid'=>100, 'name'=>'a'),
    array('id'=>102, 'parentid'=>101, 'name'=>'a'),
    array('id'=>103, 'parentid'=>101, 'name'=>'a')
);

echo '<pre>';
print_r(array_to_tree($array));
exit;

Function to convert tree.

function array_to_tree(array $array, $parent_id = 0)
{
    $array = array_combine(array_column($array, 'id'), array_values($array));

    foreach ($array as $k => &$v) {
        if (isset($array[$v['parentid']])) {
            $array[$v['parentid']]['children'][$k] = &$v;
        }
        unset($v);
    }

    return array_filter($array, function($v) use ($parent_id) {
        return $v['parentid'] == $parent_id;
    });
}

Output will be.

As per your requirement you want only 2 levels then don't use recursive function. //remove createTree from foreach

function createTree(&$list, $parent){
    $tree = array();
    foreach ($parent as $k=>$l){
        if(isset($list[$l['id']])){
            $l['children'] = $list[$l['id']]; //remove createTree from here.
        }
        $tree[] = $l;
    } 
    return $tree;
}



回答2:


This pre-processes the input data so that the order in which they are processed is in parent ID descending then ID ascending. This means that you can then just accumulate all of the menus in one pass (some comments in code)...

$array = array(
    array('id'=>100, 'parentid'=>0, 'name'=>'a'),
    array('id'=>101, 'parentid'=>100, 'name'=>'a'),
    array('id'=>102, 'parentid'=>101, 'name'=>'a'),
    array('id'=>103, 'parentid'=>101, 'name'=>'a')
);

print_r(array_to_tree($array));

function array_to_tree ( array $input ): array {
    $new = [];
    // Sort data in reverse order of parent ID, normal order of ID within parent ID
    usort($input, function ( $a, $b ) {
        return ( $a['parentid'] == $b['parentid'] )? 
            $a['id']- $b['id']:
            $b['parentid'] - $a['parentid'];
    });
    foreach ( $input as $inp )  {
        // Add data to parent ID
        $new[$inp['parentid']][$inp['id']] = $inp;
        // If data already exists for this ID, then this is also a parent
        if ( isset( $new[$inp['id']]) )   {
            // Add existing data to new item and remove old one
            $new[$inp['parentid']][$inp['id']] += $new[$inp['id']];
            unset($new[$inp['id']]);
        }
    }

    return $new;
}


来源:https://stackoverflow.com/questions/57782903/create-array-tree-of-nth-level-form-array-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!