-1

I have php order array and I'm sending this array to shipping service but my clients have 2-3 order with same shop_id. I'm sending 3th shipping this is problem must be send only 1 shipping with shop_id and must be sum(total) price.

How can I do this? My array output under below.

Array
(
    [date] => 2019-05-23
    [name] => Alex
    [shop_id] => 1
    [price] => 13.45
)

Array
(
    [date] => 2019-05-23
    [name] => Alex
    [shop_id] => 1
    [price] => 22.45
)

Array
(
    [date] => 2019-05-23
    [name] => Alan
    [shop_id] => 3
    [price] => 83.56
)

My array code:

    print("<pre>".print_r($orders,true)."</pre>");

My array must be:

   Array
    (
        [date] => 2019-05-23
        [name] => Alex
        [shop_id] => 1  // LOOK AT SAME shop_id ones in array and
        [price] => 35.90 // do total price them and delete 1 more items.
    )

    Array
    (
        [date] => 2019-05-23
        [name] => Alan
        [shop_id] => 3
        [price] => 83.56
    )

My codes;

  $newarray = array();
        foreach($arr as $ar)
        {
            foreach($ar as $k => $v)
            {
                if(array_key_exists($v, $newarray))
                    $newarray[$v]['price'] = $newarray[$v]['price'] + $ar['price'];
                else if($k == 'shop_id')
                    $newarray[$v] = $ar;
            }
        }




        print("<pre>".print_r($newarray,true)."</pre>");

Output

Array
(
)
halfer
  • 19,824
  • 17
  • 99
  • 186
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • So print_r is your attempt at solving the problem? – Andreas May 29 '19 at 10:59
  • @Andreas no , my array wrong i need to sum price values with same shop_id ones and after only 1 item must be there with same shop_id – SwiftDeveloper May 29 '19 at 11:01
  • Yes but what have you tried. You know how SO works. – Andreas May 29 '19 at 11:01
  • @dWinder https://meta.stackoverflow.com/questions/270418/answering-a-question-which-you-vote-to-close-as-off-topic – Andreas May 29 '19 at 11:13
  • Agreed - after seeing your duplicate - it is the same. So deleting my answer. Sorry SwiftDeveloper - but @Andreas right - this answer should solve your issue - if you still encounter problem, post your code and your error and we will be glad to help you – dWinder May 29 '19 at 11:16
  • @dWinder i added my codes and output. – SwiftDeveloper May 29 '19 at 11:21
  • Try this loop: `foreach($arr as $e) { if(array_key_exists($e["shop_id"], $newarray)) $newarray[$e["shop_id"]]['price'] += $e['price']; else $newarray[$e["shop_id"] = $e; }` – dWinder May 29 '19 at 11:40
  • 1
    @SwiftDeveloper Check output [here](https://3v4l.org/qnnCC) – Rahul May 29 '19 at 12:03

1 Answers1

0

You can build an result-array that is indexed by the shop-id. Then iterate over the data-array and check if that key already exists. If the key does not exists add the item to the results-array. If the item exists just update the price.

$data = [
    [
        "date"      => "2019-05-23",
        "name"      => "Alex",
        "shop_id"   => 1,
        "price"     => 12.34,
    ],
    [
        "date"      => "2019-05-23",
        "name"      => "Alex",
        "shop_id"   => 2,
        "price"     => 12.34,
    ],
    [
        "date"      => "2019-05-23",
        "name"      => "Alex",
        "shop_id"   => 1,
        "price"     => 23.45,
    ],
];

$result = [];
foreach ($data as $item) {
    $key = 'SHOP-ID-'.$item['shop_id'];
    if (!array_key_exists($key, $result)) {
        $result[$key] = $item;
    } else {
        $result[$key]['price'] += $item['price'];
    }
}

var_dump($result);
Jan
  • 2,853
  • 2
  • 21
  • 26