合并两个diamensional数组

时间:2017-01-06 09:18:07

标签: php arrays array-merge

我处理过的数组看起来像

Array
(
    [695] => Array
        (
            [0] => Array
                (
                    [0] => 00:00
                    [1] => 01:00
                    [2] => 01:30
                    [3] => 03:30
                    [4] => 04:00
                )    
        )

    [700] => Array
        (
            [1] => Array
                (
                    [0] => 00:00
                    [1] => 00:30
                    [2] => 01:00
                    [3] => 01:30
                    [4] => 02:00
                )

            [2] => Array
                (
                    [0] => 00:00
                    [1] => 00:30
                    [2] => 09:00
                    [3] => 06:30
                    [4] => 07:00                       
                )    
        )    
)

我想根据密钥合并结果。

$result = array();
foreach ($response as $key=>$list) {
  $result[$key] = array_merge($result, $list);
}
print_r($result);

但我没有得到适当的解决方案

是否有任何简单的方法2使用array_merge函数执行此操作,即基于键值在数组内合并。 期待输出像

Array
    (
        [695] => Array
            (
                        [0] => 00:00
                        [1] => 01:00
                        [2] => 01:30
                        [3] => 03:30
                        [4] => 04:00
            )

        [700] => Array
                    (
                        [0] => 00:00
                        [1] => 00:30
                        [2] => 01:00
                        [3] => 01:30
                        [4] => 02:00
                        [5] => 09:00
                        [6] => 06:30
                        [7] => 07:00                       
                    )    
    )

1 个答案:

答案 0 :(得分:2)

像这样合并数组:

$result = array_map(function($v){
  $o = [];
  foreach($v as $val)
  {
    $o = array_merge($o, $val);
  }
  return array_values(array_unique($o));
}, $array);