PHP数组按日期合并

时间:2013-04-09 10:25:57

标签: php arrays

我有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
    )

任何想法,代码片段?

4 个答案:

答案 0 :(得分:2)

试试这段代码。

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);

答案 1 :(得分:1)

尝试此功能

$array1 = array(
    array( "uregisterDate" => '2013-04-03', "total" => 4 ),
    array( "uregisterDate" => '2013-04-04', "total" => 4 ),
    array( "uregisterDate" => '2013-04-05', "total" => 3 )
   );

$array2 = array(
    array( "uregisterDate" => '2013-04-03', "totalFailed" => 2),
    array( "uregisterDate" => '2013-04-04', "totalFailed" => 3 )
   );

function merge_array_common_key($a1, $a2, $Ckey) {
    $merge = array_merge($a1,$a2);
    $keys = array();
    foreach ($merge as $key => $value) {
        if(isset($keys[$value[$Ckey]])){
            $merge[$keys[$value[$Ckey]]] += $value;
            unset($merge[$key]);
            continue;
        }
        $keys[$value[$Ckey]] = $key;
    }
    return $merge;
}

$test = merge_array_common_key($array1, $array2, 'uregisterDate');

var_dump($test);

答案 2 :(得分:0)

尝试类似:

function combine( $key, $array1, $array2 )
{
    $args = func_get_args();
    array_shift( $args );
    $return = $args[0];
    $arrays = array_slice( $args, 1 );

    foreach ( $arrays as $array ) {
        foreach ( $array as $arrayValue ) {
            $match_found = false;
            foreach ( $return as $i => $value ) {
                if ( isset( $value[$key] ) && isset( $arrayValue[$key] ) && $value[$key] == $arrayValue[$key] ) {
                    $return[$i] = array_merge( $value, $arrayValue );
                    $match_found = true;
                }
            }

            if ( !$match_found ) {
                $return[] = $arrayValue;
            }
        }
    }

    return $return;
}

$array = combine( "uregisterDate", $array1, $array2 );

答案 3 :(得分:-1)

我认为内置函数array_merge_recursive()将是gr8:

$array = array_merge_recursive($array1, $array2);

或功能可能有帮助

$newarray = Array();
foreach ($arr1 as $element=>$value){
    $newarray = array_merge($arr1[$element],$arr2[$element])
}