比较数组并将缺少的键值添加到数组

时间:2016-10-14 17:57:10

标签: php

我有两个数组:

第一

array (size=6)
0 => 
array (size=2)
  'Age' => string '25-34' (length=5)
  'Count' => string '45' (length=2)
1 => 
array (size=2)
  'Age' => string '55-64' (length=5)
  'Count' => string '1' (length=1)
 2 => 
array (size=2)
  'Age' => string '13-17' (length=5)
  'Count' => string '3' (length=1)
3 => 
array (size=2)
  'Age' => string '35-44' (length=5)
  'Count' => string '11' (length=2)
4 => 
array (size=2)
  'Age' => string '18-24' (length=5)
  'Count' => string '46' (length=2)
5 => 
array (size=2)
  'Age' => string '45-54' (length=5)
  'Count' => string '2' (length=1)

第二

array (size=5)
0 => 
array (size=2)
  'Age' => string '65+' (length=3)
  'Count' => string '1' (length=1)
1 => 
array (size=2)
  'Age' => string '13-17' (length=5)
  'Count' => string '4' (length=1)
2 => 
array (size=2)
  'Age' => string '35-44' (length=5)
  'Count' => string '3' (length=1)
3 => 
array (size=2)
  'Age' => string '25-34' (length=5)
  'Count' => string '11' (length=2)
4 => 
array (size=2)
  'Age' => string '18-24' (length=5)
  'Count' => string '20 |' (length=4)

现在我得到的是第一个数组大小比第二个大,所以我需要一个解决方案,使小尺寸数组类似于更大尺寸的数组。

使用相同的键,并将值零添加到新添加的键值。

1 个答案:

答案 0 :(得分:0)

array_merge( $firstArray, $secondArray );

请参阅array_merge();