分组并计算数组中的值

时间:2018-11-17 12:41:00

标签: php arrays

我有一个如下数组:

Array
(

    [0] => Array
        (
            [0] => Array
                (
                    [Product_Name] => Apple Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

            [1] => Array
                (
                    [Product_Name] => Pecan Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Product_Name] => Apple Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Product_Name] => Strawberry Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

            [1] => Array
                (
                    [Product_Name] => Pecan Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [Product_Name] => Lemon Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

            [1] => Array
                (
                    [Product_Name] => Pecan Pie - 8"
                    [Product_Qty] => 1
                    [Product_Cat] => Pies
                )

        )

)

我正在尝试按相同的Product_Name进行分组,并且还要在每个产品中添加Product_Qty。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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)
}

答案 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;
        }
    }
}