4

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

4 回答 4

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);
于 2013-04-09T10:48:54.603 回答
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);
于 2013-04-09T11:00:22.127 回答
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 );
于 2013-04-09T11:00:26.493 回答
-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])
}
于 2013-04-09T10:37:29.793 回答