简体   繁体   中英

PHP merge two arrays and keep keys

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:

[
    {
        "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.

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM