深度为n级的PHP数组

时间:2018-05-04 06:00:03

标签: php logic

假设我有一个数组并且递归地想要做一些任务。我花了一天时间才能解决这个问题。可能是我的逻辑不好。 任何有关如何以有效方式做这件事的帮助都会节省我的时间。

我有一个n级深的数组,看起来像:

Array
(
    [0] => Array
        (
            [name] => user1
            [email] => user1@demo.com            
            [depth] => 1            
        ) 
    [1] => Array
        (
            [name] => user2
            [email] => user2@demo.com            
            [depth] => 1 
            [0] => Array
                (
                    [0] => Array
                        (
                            [name] => user2.1
                            [email] => user2.1@demo.com                            
                            [depth] => 2
                        )
                )
        ) 
    [2] => Array
        (
            [name] => user3
            [email] => user3@demo.com             
            [depth] => 1
            [0] => Array
                (
                    [0] => Array
                        (
                            [name] => user3.1
                            [email] => user3.1@demo.com                              
                            [depth] => 2
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => user3.1.1
                                            [email] => user3.1.1@demo.com                                            
                                            [depth] => 3
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [name] => user3.2
                            [email] => user3.2@demo.com                              
                            [depth] => 2                            
                        )
                )
        )
)  

我想以这种格式改变上面的数组:

array(
        0 => array(
            'name' => 'user1',
        ),      
        1 => array(
            'name' => 'user2',      
            'children' => array(
                0 => array(
                    'name' => 'user2.1',                    
                ) ,             
            ) ,
        ) ,     
        2 => array(
            'name' => 'user3',
            'children' => array(
                0 => array(
                    'name' => 'user3.1',
                    'children' => array(
                        0 => array(
                            'name' => 'user3.1.1',                  
                        ) ,             
                    ) ,
                ) ,
                1 => array(
                    'name' => '3.2',
                )               
            ) ,
        ) ,
)

编辑: 我正在使用此代码并且工作正常,如果我想以树格式显示数据但无法按需要将数据推送到数组中。

function displayArrayRecursively($arr, $indent='') {    
    if ($arr) {
        foreach ($arr as $key => $value) {
            if (is_array($value)) {                         
                displayArrayRecursively($value, $indent . '-->');

            } else {                        
                if ($key == 'name')
                {   
                    echo "$indent $value <br>"; 
                }
                else {
                    continue;
                }                       
            }
        }

    }
}       

displayArrayRecursively($userListArray);

有谁能建议我怎么做?

谢谢

1 个答案:

答案 0 :(得分:1)

此功能可以满足您的需求:

function process_nodes($nodes) {
    $new = array();
    foreach ($nodes as $node) {
        $new[] = array('name' => $node['name']);
        if (isset($node[0])) {
            $new[count($new)-1]['children'] = process_nodes($node[0]);
        }
    }
    return $new;
}

print_r(process_nodes($data));

输出:

Array
(
    [0] => Array
        (
            [name] => user1
        )

    [1] => Array
        (
            [name] => user2
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => user2.1
                        )

                )

        )

    [2] => Array
        (
            [name] => user3
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => user3.1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => user3.1.1
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [name] => user3.2
                        )

                )

        )

)