将多维节点阵列复制到另一个阵列中

时间:2016-02-21 23:28:38

标签: php recursion multidimensional-array

我希望使用递归函数将多维数组转换为另一个多维数组。

源数组:

Array
(
    [1] => Array
        (
            [id] => 1
            [source_name] => kk56ca1d0f2378f
            [company_id] => 1
            [lft] => 1
            [rgt] => 18
            [parent_id] => 0
            [children] => Array
                (
                    [2] => Array
                        (
                            [id] => 2
                            [source_name] => kk56ca1d17f3f63
                            [company_id] => 1
                            [lft] => 2
                            [rgt] => 3
                            [parent_id] => 1
                            [children] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [id] => 3
                            [source_name] => kk56ca1d1ebe975
                            [company_id] => 1
                            [lft] => 4
                            [rgt] => 13
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [source_name] => kk56ca1fc882ac0
                                            [company_id] => 1
                                            [lft] => 5
                                            [rgt] => 10
                                            [parent_id] => 3
                                            [children] => Array
                                                (

                                                )

                                        )

                                )

                        )

                )

        )

)

我需要进入

的格式
    Array
(
    [0] => Array
        (
            [id] => 1
            [text] => kk56ca1d0f2378f
            [parent_id] => 0
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => kk56ca1d17f3f63
                            [parent_id] => 1
                            [nodes] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => kk56ca1d1ebe975
                            [parent_id] => 1
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [text] => kk56ca1fc882ac0
                                            [parent_id] => 3
                                            [nodes] => Array
                                                (

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 15
                                            [text] => kk
                                            [parent_id] => 3
                                            [nodes] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

我一直在努力工作几个小时而且无处可去。任何帮助都会非常感激。

源数组具有关联索引(尽管它们是数字),目标数组具有数字索引。除此之外,只需要删除一些索引并更改一些名称。

编辑:

具体变化:

  • 将索引名称source_name更改为文本
  • 将索引名称子项更改为节点
  • 取消设置索引lft,rgt,company_id

我对递归的经验不多,所以我一直没有尝试。

这是我能想到的:

// pass array of nodes
function convert_array($from){

    // this is a node
    if(isset($from['source_name']))
    {
        $temp = array();
        $temp['id'] = $from['id'];

        convert_array($from['children']);
    }
    // this is an array of nodes
    else
    {
        foreach($from as $arr)
        {
            $ret = convert_array($arr);
            print_r($ret);
        }

    }
}

但是我无法理解要返回的数据以及新数组如何从返回值构建。

1 个答案:

答案 0 :(得分:1)

这里的工作功能:

function convert_array( $array )
{
    $retval = array();

    foreach( $array as $row )
    {
        $child              = array();
        $child['id']        = $row['id'];
        $child['text']      = $row['source_name'];
        $child['parent_id'] = $row['parent_id'];
        if( count( $row['children'] ) )
        { $child['nodes'] = convert_array( $row['children'] ); }
        else
        { $child['nodes'] = array(); }
        $retval[] = $child;
    }
    return $retval;
}

3v4l demo

我认为这是不言自明的,BTW:我们初始化一个空数组($retval),然后我们通过所有数组参数执行foreach循环:对于每个元素,我们初始化一个新数组我们将idsource_name添加为textparent_id;如果children索引有元素,我们执行递归调用来填充nodes数组索引,否则我们将它设置为空数组。