문제

I have a trouble regarding merging arrays by specific scenario. Searching for similar cases gave no results here. To understand clearly what is my requirements, please look at the next example:

First array:

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

Second array:

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

What I need at the output:

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

So as you can see merging goes by call_date. Items from the first and the second array which are under the date 2013-10-22 00:00:00 was combined, item from the second array under the date 2013-10-24 00:00:00 was appended.

Tried a lot of combinations of array_merge, array_udiff, array_merge_recursive, array_map, but nothing helped :(.

Will appreciate for solving this problem!

도움이 되었습니까?

해결책

Simple scenario:

  1. change array keys
  2. merge recursive
  3. map merged elements back to object (stdClass)

Example:

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

output:

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
        )

)

다른 팁

$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);
$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. Define relevant arrays like $arr1 and $arr2
  2. array_merge() them
  3. Create a new array with union logic on 'call_date'
  4. Remove the date as key through array_values()
$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);

if you have more keys add on amounts array, if you want replace existing keys remove !isset($f[$amount]) && from condition and you have more than one equal call_date remove break; too.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top