按列对 3d 数组最低级别的数据进行分组,并对每组中的另一列求和以创建平面关联数组

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

我需要按一个深列对 3d 数组中的数据进行分组,并对另一个深列求和以生成一个平面关联数组,其中包含唯一值及其总和。

[
  [
    [
      "Product_Name" => "Apple Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    [
      "Product_Name" => "Apple Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  [
    [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  [
    [
      "Product_Name" => "Strawberry Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  [
    [
      "Product_Name" => "Lemon Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ]
];

我正在尝试按相同的

Product_Name
进行分组,并将每组中的
Product_Qty
加起来。

想要的结果:

[
  'Apple Pie - 8' => 2,
  'Pecan Pie - 8' => 3,
  'Strawberry Pie - 8' => 1,
  'Lemon Pie - 8' => 1,
]
php arrays multidimensional-array sum grouping
3个回答
2
投票

您可以创建一个双循环,并将不同数组键下每个不同产品的所有值相加,如下所示:

$myArr = [
  0 => [
    0 => [
      "Product_Name" => "Apple Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    1 => [
      "Product_Name" => "Apple Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  1 => [
    0 => [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  2 => [
    0 => [
      "Product_Name" => "Strawberry Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    1 => [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ],
  3 => [
    0 => [
      "Product_Name" => "Lemon Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ],
    1 => [
      "Product_Name" => "Pecan Pie - 8",
      "Product_Qty" => 1,
      "Product_Cat" => "Pies"
    ]
  ]
];

$total = [];

foreach($myArr as $group){
  foreach($group as $item){
    $total[$item["Product_Name"]] = isset($total[$item["Product_Name"]]) ? $total[$item["Product_Name"]] + $item["Product_Qty"] : $item["Product_Qty"];
  }
}

var_dump($total);

它将提供总计:

array(4) {
  ["Apple Pie - 8"]=>
  int(2)
  ["Pecan Pie - 8"]=>
  int(3)
  ["Strawberry Pie - 8"]=>
  int(1)
  ["Lemon Pie - 8"]=>
  int(1)
}

0
投票

如果您现在想获取数组中的所有数据,则可以运行以下代码。

$finalArray = array();
$product_name_arr = array();
foreach ($array as $key => $value) {
    foreach ($value as $sub_key => $sub_value) {
        if(in_array($sub_value['Product_Name'], $product_name_arr)){
            foreach ($finalArray as $fa_key => $fa_value) {
                if($fa_value['Product_Name'] == $sub_value['Product_Name']){
                    $finalArray[$fa_key]['Product_Qty'] = $finalArray[$fa_key]['Product_Qty'] + $sub_value['Product_Qty'];
                }
            }
        } else {
            $product_name_arr[] = $sub_value['Product_Name'];
            $finalArray[] = $sub_value;
        }
    }
}

0
投票

因为第一层可以安全地展平,所以使用“扩展和合并”技术可以消除对嵌套循环的需要。下面使用“数组解构”来仅访问将使用的列值。

代码:(演示

$result = [];
foreach (
    array_merge(...$array)
    as
    ['Product_Name' => $n, 'Product_Qty' => $q]
) {
    $result[$n] = ($result[$n] ?? 0) + $q;
}
var_export($result);

如果没有展平,具有arrat解构的嵌套循环可能如下所示:(演示

$result = [];
foreach ($array as $set) {
    foreach ($set as ['Product_Name' => $n, 'Product_Qty' => $q]) {
        $result[$n] = ($result[$n] ?? 0) + $q;
    }
}
var_export($result);
© www.soinside.com 2019 - 2024. All rights reserved.