合并两个数组,并通过一个字段组合重复项

时间:2013-10-29 07:50:45

标签: php arrays merge stdclass

我遇到了按特定方案合并数组的问题。搜索类似的案例在这里没有结果。要清楚地了解我的要求,请查看下一个示例:

第一个阵列:

Array
(
    [0] => stdClass
        (
            [call_date] => 2013-10-22 00:00:00
            [first_amount] => 10
        )

    [1] => stdClass
        (
            [call_date] => 2013-10-23 00:00:00
            [first_amount] => 20
        )
)

第二阵列:

Array
(
    [0] => stdClass
        (
            [call_date] => 2013-10-22 00:00:00
            [second_amount] => 30
        )

    [1] => stdClass
        (
            [call_date] => 2013-10-24 00:00:00
            [second_amount] => 40
        )
)

我在输出中需要的是:

Array
(
    [0] => stdClass
        (
            [call_date] => 2013-10-22 00:00:00
            [first_amount] => 10
            [second_amount] => 30
        )

    [1] => stdClass
        (
            [call_date] => 2013-10-23 00:00:00
            [first_amount] => 20
        )

    [2] => stdClass
        (
            [call_date] => 2013-10-24 00:00:00
            [second_amount] => 40
        )
)

因此,您可以看到合并是通过 call_date 进行的。组合了第一个和第二个数组中2013-10-22 00:00:00之后的项目,附加了日期为2013-10-24 00:00:00的第二个数组中的项目。

尝试了很多 array_merge,array_udiff,array_merge_recursive,array_map 的组合,但没有任何帮助:(。

很高兴能解决这个问题!

4 个答案:

答案 0 :(得分:1)

简单方案:

  1. 更改数组键
  2. merge recursive
  3. 将合并后的元素映射回对象(stdClass)
  4. 示例:

    //change key
    $workFirstArray = array_combine(
        array_map(function($object) { return $object->call_date;}, $firstArray), $firstArray
    );
    $workSecondArray = array_combine(
        array_map(function($object) { return $object->call_date;}, $secondArray), $secondArray
    );
    
    //map merged elements back to StdClass
    $result = array_map(function($element) {
            if(is_array($element)) {
                $element['call_date'] = end($element['call_date']);
                $element=(object)$element;
            }
            return $element;
        },
        array_merge_recursive($workFirstArray, $workSecondArray)
    );
    

    输出:

    Array
    (
        [0] => stdClass Object
            (
                [call_date] => 2013-10-22 00:00:00
                [first_amount] => 10
                [second_amount] => 40
            )
    
        [1] => stdClass Object
            (
                [call_date] => 2013-10-23 00:00:00
                [second_amount] => 30
            )
    
        [2] => stdClass Object
            (
                [call_date] => 2013-10-24 00:00:00
                [second_amount] => 40
            )
    
    )
    

答案 1 :(得分:0)

$arr1 = array(  array(  'call_date'    => '2013-10-22 00:00:00',
                        'first_amount' => 10),
                array(  'call_date'    => '2013-10-23 00:00:00',
                        'first_amount' => 20));

$arr2 = array(  array(  'call_date'     => '2013-10-22 00:00:00',
                        'second_amount' => 30),
                array(  'call_date'     => '2013-10-24 00:00:00',
                        'second_amount' => 40));

$arr_merged = array_merge($arr1, $arr2);

$arr_result = array();
foreach ($arr_merged as $arr) {

    if ( ! isset($arr_result[$arr['call_date']]))
        $arr_result[$arr['call_date']] = array();

    $arr_result[$arr['call_date']] += $arr;

}

$arr_result = array_values($arr_result);
  1. 定义相关数组,例如$arr1$arr2
  2. array_merge() them
  3. 'call_date'
  4. 上创建一个包含union logic的新数组
  5. 通过array_values()
  6. 删除日期为关键字

答案 2 :(得分:0)

$result = array();
$result = $firstArray;
for ($i=0; $i < count($firstArray); $i++){
    for ($j=0; $j < count($secondArray); $j++){
        if ($firstArray[$i]['call_date'] == $secondArray[$j]['call_date']){
            $result[$i]['second_amount'] = $secondArray[$j]['second_amount'];
            break;
        }
    }
    if ($j == count($secondArray))
        $result[] = $secondArray[$j-1];
}
var_dump($result);

答案 3 :(得分:0)

$result = $first;
$amounts = array('first_amount','second_amount');
foreach ($first as $fKey => $f) {
    foreach ($second as $sKey => $s) {
        if ($f['call_date'] == $s['call_date']) {
            foreach ($amounts as $amount) {
                if (!isset($f[$amount]) && isset($s[$amount]))
                    $result[$fKey][$amount] = $s[$amount];
            }
            unset($second[$sKey]);
            break;
        }
    }
}
$result = array_merge($result, $second);

如果你有更多的键添加金额数组,如果你想要替换现有的密钥从条件中删除!isset($f[$amount]) &&,你有多个相等的call_date也删除break;