PHP数组按日期合并

我有2个数组,每个数组有2个属性

第一个数组:

排列
(

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [total] => 4
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [total] => 4
    )

[2] =>; Array
    (
        [uregisterDate] => 2013-04-05
        [total] => 3
    )

)

第二个数组:

排列
(

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [totalFailed] => 2
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [totalFailed] => 4
    )

)

我希望最后的数组像下面的输出|(两个数组之间的关键uregistredDate合并:

排列
(

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [total] => 4
        [totalFailed] => 2
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [total] => 4
    [totalFailed] => 4
    )

[2] =>; Array
    (
        [uregisterDate] => 2013-04-05
        [total] => 3
    )

)

任何想法,代码片段?

最佳答案 试试这段代码.

function combo($array1, $array2) {
    foreach($array1 as $key => $value) {
        if(isset($array2[$key]))
        $result[$key] = array_merge($value, $array2[$key]);
        else
        $result[$key] = $value;
    }
    return $result;
}
$array = combo(#your array 1#, #your array 2#);
print_r($array); // to view the output

更新的代码

function combo($array1, $array2) {
    foreach($array1 as $key => $value) {
        if(isset($array2[$key])) {
        if($value['uregisterDate'] == $array2[$key]['uregisterDate'])
           {
            $result[$key] = array_merge($value, $array2[$key]);
            } 
            else
            {
                $result[$key] = $array2[$key];
                $result[rand(0,99)]= $value;
            }

        }
        else
        $result[$key] = $value;
    }
    return array_values($result);
}
$array = combo($a1, $a2);
print_r($array);
点赞