带键的重复php数组

时间:2015-05-07 18:09:54

标签: php arrays

我有两个这样的数组:

[day0_units] => 0
[day1_units] => 12
[day1_start] => 08:00:00
[day1_end] => 11:00:00
[day2_units] => 12
[day2_start] => 07:00:00
[day2_end] => 11:00:00
[day3_units] => 0
[day4_units] => 12
[day4_start] => 10:00:00
[day4_end] => 12:00:00
[day5_units] => 12
[day5_start] => 08:00:00
[day5_end] => 03:00:00
[day6_units] => 0

[day0_units] => 0
[day0_start] => 10:00:00
[day0_end] => 01:00:00
[day1_units] => 12
[day1_start] => 08:00:00
[day1_end] => 11:00:00
[day2_units] => 12
[day2_start] => 07:00:00
[day2_end] => 11:00:00
[day3_units] => 0
[day3_start] => 
[day3_end] => 
[day4_units] => 12
[day4_start] => 10:00:00
[day4_end] => 12:00:00
[day5_units] => 12
[day5_start] => 08:00:00
[day5_end] => 03:00:00
[day6_units] => 0

我想要做的就是复制数组键,两个数组在相同位置应该具有相同的键,并保留值(如果有的话)。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

从两者获取密钥并合并它们,然后翻转并与数组合并:

$keys = array_flip(array_merge(array_keys($array1), array_keys($array2)));
$array1 = array_merge($keys, $array1);
$array2 = array_merge($keys, $array2);