重新排列 php 多维数组分组/求和值

问题描述 投票:0回答:1

我有一个这样的数组:

Array
(
    [0] => Array
        (
            [date] => 20230320
            [model] => ABC
            [quantity] => 1
        )

    [1] => Array
        (
            [date] => 20230320
            [model] => DEF
            [quantity] => 2
        )

    [2] => Array
        (
            [date] => 20230320
            [model] => ABC
            [quantity] => 3
        )

    [3] => Array
        (
            [date] => 20230320
            [model] => DEF
            [quantity] => 5
        )

    [4] => Array
        (
            [date] => 20230321
            [model] => ABC
            [quantity] => 1
        )

)

我想重组它,得到这个(添加日期级别和分组模型总和):

Array
(
    [20230320] => Array
        (
            [0] => Array
                (
                    [date] => 20230320
                    [model] => ABC
                    [quantity] => 4
                )

            [1] => Array
                (
                    [date] => 20230320
                    [model] => DEF
                    [quantity] => 7
                )

        )

    [20230321] => Array
        (
            [0] => Array
                (
                    [date] => 20230321
                    [model] => ABC
                    [quantity] => 1
                )

        )

)

经过几个小时的努力,我已经能够逐步实现我的目标,但我想知道是否存在更简单的解决方案。

这是完整的工作代码:

// INPUT DATA
$orders = array(
        array(
        "date"  =>  "20230320",
        "model" =>  "ABC",
        "quantity"  => 1
        ),
        array(
        "date"  =>  "20230320",
        "model" =>  "DEF",
        "quantity"  => 2
        ),
        array(
        "date"  =>  "20230320",
        "model" =>  "ABC",
        "quantity"  => 3
        ),
        array(
        "date"  =>  "20230320",
        "model" =>  "DEF",
        "quantity"  => 5
        ),      
        array(
        "date"  =>  "20230321",
        "model" =>  "ABC",
        "quantity"  => 1
        )
);

print_r ($orders);

// STEP 1 - GROUP ITEMS BY DATE

$orders2 = array();

foreach($orders as $order) {
    $date = $order['date'];
    $orders2[$date][] = $order;
} 

print_r ($orders2);

// STEP 2 - GROUP ITEMS BY MODEL AND SUM QUANTITY

$orders3 = array();

foreach($orders2 as $items) {
    foreach($items as $item) {
        $date = $item['date'];
        $model = $item['model'];
        if(empty($orders3[$date][$model])) {
            $orders3[$date][$model][] = $item;
        } else {
            $orders3[$date][$model][0]['quantity'] += $item['quantity'];    
        }
        
    } 
}

print_r ($orders3);

// STEP 3 - REMOVE NOT NECESSARY MODEL LEVEL

$orders4 = array();

foreach($orders3 as $items) {
    foreach($items as $item) {
        $date = $item[0]['date'];

        $orders4[$date][] = $item[0];
        
    } 
}

print_r ($orders4);

你有什么建议?有没有更简单的方法以更直接的方式获得相同的结果?

提前致谢

php arrays multidimensional-array
1个回答
0
投票

您可以执行以下操作来实现此目的:

  1. 唯一的日期并为预期的键翻转数组
  2. 遍历数组并查询关联日期的键
  3. 独一无二的模型
  4. 循环并对数量求和

# Unique and flip the keys as OP expected output
$dates = array_map(fn() => [], array_flip(array_unique(array_column($orders, 'date'))));

# Walk the keys, zval the value
array_walk($dates, function (array &$value, int $key) use ($orders) {
    # Match dates
    $query = array_filter($orders, fn($order) => (int) $order['date'] === $key);

    # Get model varients
    $models = array_unique(array_column($query, 'model'));
    
    # Sum the quantities and build the array
    $value = array_map(function($model) use ($query, $key) {
        $quantity = array_sum(array_column(array_filter($query, fn($q) => $q['model'] === $model), 'quantity'));
        
        return [
            'date' => $key,
            'model' => $model,
            'quantity' => $quantity,
        ];
    }, $models);
});

# Your expected output
var_dump($dates);

看到它在 3v4l.org 上工作

© www.soinside.com 2019 - 2024. All rights reserved.