-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
(
)
8
  • So print_r is your attempt at solving the problem? Commented May 29, 2019 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 Commented May 29, 2019 at 11:01
  • Yes but what have you tried. You know how SO works. Commented May 29, 2019 at 11:01
  • @dWinder meta.stackoverflow.com/questions/270418/… Commented May 29, 2019 at 11:13
  • 1
    @SwiftDeveloper Check output here Commented May 29, 2019 at 12:03

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.