如何从原始数组安排预期的新数组

时间:2019-06-04 09:48:47

标签: php

当三个键(USERID,DATE,ITEM DETAIL)的值在数组的另一个索引中相同时,则需要添加QUANTITY的值,请参见下面的预期数组。
原始数组如下:

Array
(
    [0] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-08'
            [ITEM DETAIL] => 'Claims Investigation Hours'
            [QUANTITY] => 3.00
        )

    [1] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Hours'
            [QUANTITY] => 4.90
        )

    [2] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Hours'
            [QUANTITY] => 1.20
        )

    [3] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Travel Hours'
            [QUANTITY] => 4.40
        )

    [4] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Travel Hours'
            [QUANTITY] => 3.20
        )
)

此处:索引2和索引3中三个键(USERID,DATE,ITEM DETAIL)的值相同,因此QUANTITY键的总和值如:4.90 + 1.20

类似地,索引3和索引4中的三个键(USERID,DATE,ITEM DETAIL)相同,因此QUANTITY键的总和值如4.40 + 3.20

我想以此方式创建一个预期的数组。请,有人可以帮我吗? 期望的数组如下:

Array
(
    [0] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-08'
            [ITEM DETAIL] => 'Claims Investigation Hours'
            [QUANTITY] => 3.00
        )

    [1] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Hours'
            [QUANTITY] => 5.10
        )

    [2] => Array
        (
            [USERID] => 63120
            [DATE] => '2018-01-09'
            [ITEM DETAIL] => 'Claims Investigation Travel Hours'
            [QUANTITY] => 7.60
        )
)

3 个答案:

答案 0 :(得分:2)

这应该有效。

$final = [];

foreach ($arr as $a) {
    // generating a unique key based on the required conditions
    $key = $a['USER_ID'] . $a['DATE'] . $a['ITEM DETAIL'];
    // if not set for unique record then set the first record
    if (!isset($final[$key])) {
        $final[$key] = $a;
    } else {
        // increment the quantity for repeats 
        $final[$key]['QUANTITY'] += $a['QUANTITY'];
    }
}

$final = array_values($final);

print_r($final);

输出

Array
(
    [0] => Array
        (
            [USERID] => 63120
            [DATE] => 2018-01-08
            [ITEM DETAIL] => Claims Investigation Hours
            [QUANTITY] => 3
        )

    [1] => Array
        (
            [USERID] => 63120
            [DATE] => 2018-01-09
            [ITEM DETAIL] => Claims Investigation Hours
            [QUANTITY] => 6.1
        )

    [2] => Array
        (
            [USERID] => 63120
            [DATE] => 2018-01-09
            [ITEM DETAIL] => Claims Investigation Travel Hours
            [QUANTITY] => 7.6
        )

)

答案 1 :(得分:1)

这也可以通过减少数组来实现

$array = array_values(array_reduce($array, function($a, $b) {
    $key = implode("|", [$b['USERID'], $b['DATE'], $b['ITEM DETAIL']]);
    if(array_key_exists($key, $a)) {
        $a[$key]['QUANTITY']+= $b['QUANTITY'];
    } else {
        $a[$key] = $b;
    }

    return $a;
}, []));

答案 2 :(得分:0)

这是代码段,请参见内联文档以获取说明

$result = [];
$i = 0;
array_multisort(array_column($arr,'USERID'), SORT_ASC, 
array_column($arr,'DATE'), SORT_ASC,array_column($arr,
'ITEM DETAIL'),SORT_ASC, $data);

foreach ($arr as $key => $value) {
    if (!empty($arr[$key + 1])) { // checking for last if not empty
        // checking your condition
        if ($arr[$key]['USERID'] == $arr[$key + 1]['USERID'] &&
            $arr[$key]['DATE'] == $arr[$key + 1]['DATE'] &&
            $arr[$key]['ITEM DETAIL'] == $arr[$key + 1]['ITEM DETAIL']) 
        {
            // assigning current value to result
            $result[$i] = $value;
            // add next value to current
            $result[$i]['QUANTITY'] += $arr[$key + 1]['QUANTITY'];
            $i++;
        }
    }
}
print_r($result);