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

时间:2021-05-31 12:22:10

I'm not able to get this code working:

我无法使这段代码正常工作:

$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)))));

I get:

我明白了:

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

So 2014 have been delete by the merge of this 2 arrays. I would like to get:

所以2014年已被这两个阵列的合并删除。我想得到:

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

At least I want to send this tree by json using json_encode, or a better way if you know one.

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

2 个解决方案

#1


1  

Try this code, I have change little code of yours and add some extra paths.

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

$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)));

Output will be like this:--

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

Hope this will help you...

#2


0  

You can use following;

你可以使用以下;

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

Here is a working demo: codepad

这是一个有效的演示:codepad

#1


1  

Try this code, I have change little code of yours and add some extra paths.

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

$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)));

Output will be like this:--

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

Hope this will help you...

#2


0  

You can use following;

你可以使用以下;

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

Here is a working demo: codepad

这是一个有效的演示:codepad