Combine multidimensional array on single duplicate value and add other values

只谈情不闲聊 提交于 2019-12-24 19:57:38

问题


I have a list of line items to an order, but the feed we receive divides multiple quantities of a single item into separate line items.

I need to combine and/or certain elements if the sku is the same and only return one line item for each sku. I need to sum tax, shipping and shipping tax for each item.

[Sku]=> 47069
[QTY]=> 1
[Price]=> 5.9900
[Tax]=> 0.4200
[Shipping]=> 2.5800
[ShippingTax]=> 0.1800


[Sku]=> 241573
[QTY]=> 1
[Price]=> 6.4900
[Tax]=> 0.4700
[Shipping]=> 0.9300
[ShippingTax]=> 0.0700


[Sku]=> 241573
[QTY]=> 1
[Price]=> 6.4900
[Tax]=> 0.4500
[Shipping]=> 0.9200
[ShippingTax]=> 0.0600


[Sku]=> 241573
[QTY]=> 1
[Price]=> 6.4900
[Tax]=> 0.4500
[Shipping]=> 0.9200
[ShippingTax]=> 0.0600

This should result in

[Sku]=> 47069
[QTY]=> 1
[Price]=> 5.9900
[Tax]=> 0.4200
[Shipping]=> 2.5800
[ShippingTax]=> 0.1800

[Sku]=> 241573
[QTY]=> 3
[Price]=> 6.4900
[Tax]=> 1.3700
[Shipping]=> 2.7800
[ShippingTax]=> 0.1900

回答1:


You need to sum other values using foreach

$products[] = array(
                'Sku'=> 47069,
                'QTY'=> 1,
                'Price'=> 5.9900,
                'Tax'=> 0.4200,
                'Shipping'=> 2.5800,
                'ShippingTax'=> 0.1800,
            );

$products[] = array(
                'Sku'=> 241573,
                'QTY'=> 1,
                'Price'=> 6.4900,
                'Tax'=> 0.4700,
                'Shipping'=> 0.9300,
                'ShippingTax'=> 0.0700,
            );

$products[] = array(
                'Sku'=> 241573,
                'QTY'=> 1,
                'Price'=> 6.4900,
                'Tax'=> 0.4500,
                'Shipping'=> 0.9200,
                'ShippingTax'=> 0.0600,
            );

$products[] = array(
                'Sku'=> 241573,
                'QTY'=> 1,
                'Price'=> 6.4900,
                'Tax'=> 0.4500,
                'Shipping'=> 0.9200,
                'ShippingTax'=> 0.0600,
            );


$pr = array();
foreach($products as $product) 
{
    if(isset($pr[$product['Sku']]))
    {
        $pr[$product['Sku']]['QTY'] += $product['QTY'];
        $pr[$product['Sku']]['Tax'] += $product['Tax'];
        $pr[$product['Sku']]['Shipping'] += $product['Shipping'];
        $pr[$product['Sku']]['ShippingTax'] += $product['ShippingTax'];

    }
    else
    {
        $pr[$product['Sku']] = $product;
    }

}

OUTPUT:

Array
(
    [47069] => Array
        (
            [Sku] => 47069
            [QTY] => 1
            [Price] => 5.99
            [Tax] => 0.42
            [Shipping] => 2.58
            [ShippingTax] => 0.18
        )

    [241573] => Array
        (
            [Sku] => 241573
            [QTY] => 3
            [Price] => 6.49
            [Tax] => 1.37
            [Shipping] => 2.77
            [ShippingTax] => 0.19
        )

)



回答2:


You can use this function as a starting point.

function combine_two_arrs($arr1, $arr2, $k='Sku') {
  $arr_final = [];
  if ($arr1[$k] == $arr2[$k]){
    $arr_final[$k] = $arr1[$k];
  } else {
    return [$arr1, $arr2];
  }
  foreach($arr1 as $k1 => $v1) {
    foreach($arr2 as $k2 => $v2) {
      if(($k1 != $k) && ($k1 == $k2)){
        $arr_final[$k1] = $v1 + $v2;
      }
    }
  }
  return $arr_final;
}

Then you can apply high order functions (array_reduce might be a good candidate) to apply this function to $products array.

print_r(combine_two_arrs($products[0], $products[2], 'Sku'));
/*
Array
(
    [0] => Array
        (
            [Sku] => 47069
            [QTY] => 1
            [Price] => 5.99
            [Tax] => 0.42
            [Shipping] => 2.58
            [ShippingTax] => 0.18
        )

    [1] => Array
        (
            [Sku] => 241573
            [QTY] => 1
            [Price] => 6.49
            [Tax] => 0.45
            [Shipping] => 0.92
            [ShippingTax] => 0.06
        )

) 
*/

print_r(combine_two_arrs($products[1], $products[2], 'Sku'));

/*
    Array
(
    [Sku] => 241573
    [QTY] => 2
    [Price] => 12.98
    [Tax] => 0.92
    [Shipping] => 1.85
    [ShippingTax] => 0.13
)
*/


来源:https://stackoverflow.com/questions/51320014/combine-multidimensional-array-on-single-duplicate-value-and-add-other-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!