phparraysrecursionmerge

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


Solution

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