简体   繁体   中英

Group and Calculate Values in Array

I have an array like the following:

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
                )

        )

)

I'm trying to group by same Product_Name and also add up the Product_Qty in each. Any help is greatly appreciated.

You can create a double loop and sum all value of every different product under a different array key as below:

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

It will provide that total:

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

If you want to get all data as in array now then you can run below code.

$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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM