PHP将数组推入spessific键

时间:2015-09-08 17:11:01

标签: php arrays

我有这两个2D维数组,

$ ARRAY1

Array(
 'week1' => Array (
              0 => '2015-06-29',
              1 => '2015-06-30',
            )
)

$数组2

Array(
 'week1' => Array (
              0 => '2015-07-01',
              1 => '2015-07-02',
              2 => '2015-07-03',
              3 => '2015-07-04',
              4 => '2015-07-05',
            ),
 'week2' => Array (
              0 => '2015-07-06',
              1 => '2015-07-07',
              2 => '2015-07-08',
              3 => '2015-07-09',
              4 => '2015-07-10',
              5 => '2015-07-11',
              6 => '2015-07-12',
            ),
)

这是我的预期结果,

Array(
 'week1' => Array (
              0 => '2015-06-29',
              1 => '2015-06-30',
              2 => '2015-07-01',
              3 => '2015-07-02',
              4 => '2015-07-03',
              5 => '2015-07-04',
              6 => '2015-07-05',
            ),
 'week2' => Array (
              0 => '2015-07-06',
              1 => '2015-07-07',
              2 => '2015-07-08',
              3 => '2015-07-09',
              4 => '2015-07-10',
              5 => '2015-07-11',
              6 => '2015-07-12',
            ),
)

我一直在尝试使用array_push,但数组变为3维而不是加入相同的键名。你们可以叫我出去吗? 提前致谢。 :d

2 个答案:

答案 0 :(得分:4)

我认为你可以使用array_merge_recursive(http://php.net/manual/en/function.array-merge-recursive.php

只需:

array_merge_recursive($array1, $array2)

答案 1 :(得分:1)

只需循环遍历您要添加的数组并使用数组array_merge()的键,例如

foreach($array1 as $k => $v)
    $array2[$k] = array_merge($array2[$k], $v);