2014-03-29 64 views
0

我不能得到这个代码的工作:PHP合并两个数组并保持键

$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已经通过这2个数组的合并删除。我想获得:

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

至少我希望通过JSON使用json_encode,或者更好的办法,如果你知道一个发送此树。

+0

您能给什么是你想要做更详细?你的输出是没有意义的。 '04'从哪里来? –

+0

对不起04是一个错误...我有一个路径数组,然后我在'/'上分割每个路径,因为我想得到一个目录树。 – user3475649

+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: [ ] 
     } 
    ] 
} 
] 

希望这将帮助你...

+0

感谢您的快速回答!但结果是不是像预期的那样,我想确切的JSON我显示上 – user3475649

+0

看到我更新的答案。 –

+0

结果是完美的!但是我的代码出现了这个错误:S trict标准:只有变量应该通过引用传递 – user3475649

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

+0

坦克为您的答案!结果非常接近我想要做的事情。目录2014出现在您的解决方案中两次,并使树错了。 – user3475649