PHP合并两个数组并保留密钥

时间:2014-03-29 12:22:32

标签: php arrays merge key

我无法使此代码正常工作:

$paths = ['2014/','2014/04/','2015/'];
$tree = array();
    foreach ($paths as $path) {
        $dir_exploded = explode("/", $path);
        array_pop($dir_exploded);

        $tmp = array();
        for ($i = count($dir_exploded) - 1; $i >= 0; $i--) {
            if ($i == count($dir_exploded) - 1) {
                $children = array();
            } else {
                $children = array($tmp);
            }
            $tmp = array('text' => $dir_exploded[$i], 'children' => $children);
        }

        $tree = array_replace_recursive($tree, $tmp);
    }
echo(json_encode(array(array('text' => '/', 'children' => array($tree)))));

我明白了:

[
    {
        "text": "/",
        "children": [
            {
                "text": "2015",
                "children": [
                    {
                        "text": "04",
                        "children": []
                    }
                ]
            }
        ]
    }
]

因此,这两个数组的合并删除了2014年。我想得到:

[
    {
        "text": "/",
        "children": [
            {
                "text": "2014",
                "children": [
                    {
                        "text": "04",
                        "children": []
                    }
                ]
            },{
                "text": "2015",
                "children": []
            }
        ]
    }
]

至少我想通过json使用json_encode发送这个树,或者如果你知道一个更好的方法。

2 个答案:

答案 0 :(得分:1)

试试这段代码,我已经更改了你的一些代码,并添加了一些额外的路径。

$paths = array('2014/', '2014/04/','2014/03/','2015/');
$new_tree = $temp_new_tree =  array();
foreach ($paths as $path)
{
    $dir_exploded = explode("/", $path);
    array_pop($dir_exploded);

    $temp_new_tree = (!empty($new_tree)) ? $new_tree : array();
    $tmp = $tree = array();
    for ($i = count($dir_exploded) - 1; $i >= 0; $i--)
    {
        if ($i == count($dir_exploded) - 1) {
            $children = array();
        } else {
            $children = array($tmp);
        }
        $tmp = array('text' => $dir_exploded[$i], 'children' => $children);
    }

    $tree = array_replace_recursive($tree, $tmp);

    $new_tree[$dir_exploded[0]] = $tree;
    if(array_key_exists($dir_exploded[0], $temp_new_tree))
    {
        $new_tree[$dir_exploded[0]]['children'] = array_merge($new_tree[$dir_exploded[0]]['children'], $temp_new_tree[$dir_exploded[0]]['children']);
    }
}
//echo json_encode(array(array('text' => '/', 'children' => array($new_tree))));

$new_tree = array_shift(array_chunk($new_tree, count($new_tree)));
echo json_encode(array(array('text' => '/', 'children' => $new_tree)));

输出将如下: -

[
{
    text: "/",
    children: [
        {
            text: "2014",
            children: [
            {
                text: "03",
                children: [ ]
            },
            {
                text: "04",
                children: [ ]
            }
            ]
        },
        {
            text: "2015",
            children: [ ]
        }
    ]
}
]

希望这会对你有帮助......

答案 1 :(得分:0)

您可以使用以下内容;

<?php
$paths = array('2014/01/02','2014/04/03','2015/07');

$all = array();
foreach ($paths as $path) {
    $temp = explode("/", $path);
    $all[] = tree($temp, 0);

}

var_dump($all);

function tree($arr, $i) {
    if (count($arr) == ($i + 1)) return $arr[$i];
    return array(
            "text" => $arr[$i],
            "children" => tree($arr, $i+1)
        );
}

以下是一个有效的演示: codepad