2

我在按特定场景合并数组时遇到了麻烦。搜索类似的案例在这里没有结果。要清楚地了解我的要求是什么,请看下一个示例:

第一个数组:

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

4 回答 4

1

简单场景:

  1. 更改数组键
  2. 合并递归
  3. 将合并的元素映射回对象(stdClass)

例子:

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

)
于 2013-10-29T08:57:39.663 回答
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_datebreak;也删除。

于 2013-10-29T13:07:48.663 回答
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()他们
  3. 使用联合逻辑创建一个新数组'call_date'
  4. 删除日期作为关键通过array_values()
于 2013-10-29T08:27:38.387 回答
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);
于 2013-10-29T08:29:58.383 回答