数组是这样的
Array 1
(
[2014-07-01] => Array
(
[date] => 2014-07-01
[totalr] => 13
)
[2014-07-02] => Array
(
[date] => 2014-07-02
[totalr] => 18
)
//... one for each day of the month
)
然后我有另一个数组与之前的非常相似唯一改变的键是键"total"
Array 2
(
[2014-07-01] => Array
(
[date] => 2014-07-01
[members] => 21
)
[2014-07-02] => Array
(
[date] => 2014-07-02
[members] => 30
)
//... one for each day of the month
)
对于这两个数组,我需要创建一个单独的数组,所以最终的数组应该是这样的:
Final Array
(
[2014-07-01] => Array
(
[date] => 2014-07-01
[totalr] => 13
[members] => 21
)
[2014-07-02] => Array
(
[date] => 2014-07-02
[totalr] => 18
[members] => 30
)
// One for each day of the month
)
每个块的键对于每个数组都是相同的…我已经尝试了array_merge没有工作…那么我该怎么做呢?
UPDATE - 8-13-2014 08:12
基于@ChicagoRedSox评论,我找到了这个函数:
function juntaArrs($arr1, $arr2) {
if (!is_array($arr1) or !is_array($arr2)) { return $arr2; }
foreach ($arr2 AS $sKey2 => $sValue2) {
$arr1[$sKey2] = juntaArrs(@$arr1[$sKey2], $sValue2);
}
return $arr1;
}
$new_arr = juntaArrs($a1, $a2);
print_r($new_arr);
它工作完美!!谢谢你。
// start with a new array
$newArr = array();
// for every key and value in array 1
foreach($arr1 as $k=>$v){
// add in the new array at key each key an array with date (the key),
// totalr (the second value in the sub-array) and numbers (the second value in
// the array 2 at the key position
$newArr[$k] = array('date'=>$k, 'totalr'=>$v[1], 'numbers'=>$arr2[$k][1]);
}
// it's what we obtain
print_r($newArr);