PHP合并两个数组并保留键

PHP合并两个数组并保留键,php,arrays,merge,key,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; $

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

$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_encode通过json发送这棵树,或者如果你知道的话,用更好的方式发送

<?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)
        );
}

试试这段代码,我对你的代码做了一些修改,并添加了一些额外的路径

$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)));
输出如下:--
希望这能对你有所帮助……你能详细说明你想做什么吗?你的输出毫无意义。
04
是从哪里来的?对不起,04是个错误。。。我有一个路径数组,然后我在“/”上拆分每个路径,因为我想得到一个目录树。请查看我的答案并将其降级!结果和我想做的非常接近。目录2014在您的解决方案中出现两次,并使树出错。感谢您的快速回答!但是结果与预期的不一样,我希望得到确切的json,我显示upperResult是完美的thx u!但是我在您的代码中遇到了这个错误:严格的标准:只有变量应该通过引用传递错误可能是因为空数组。它的
注意
警告
…所以没有问题之前的
。所以在
$tree=array\u replace\u recursive($tree,$tmp)$tree为空。非常感谢;)
[
{
    text: "/",
    children: [
        {
            text: "2014",
            children: [
            {
                text: "03",
                children: [ ]
            },
            {
                text: "04",
                children: [ ]
            }
            ]
        },
        {
            text: "2015",
            children: [ ]
        }
    ]
}
]