0

As you can see, I have an array with duplicate orderid's because one order can have multiple stockitems(stockitemname). How can I merge the array, so that the total field is summed up, the orderid's are not duplicate anymore and the all the stockitemnames are placed under the same orderId?

Does anyone have a solution to solve this?

Array ( [0] => Array 
       ([orderId] => 1544100294 
        [total] => 215.28 
        [receivedate] => 0000-00-00 
        [stockitemname] => "The Gu" red shirt XML tag t-shirt (White) XXS 
        [customerid] => 10 ) 
    [1] => Array 
       ( [orderId] => 1544119651 
        [total] => 37.38 
        [receivedate] => 0000-00-00 
        [stockitemname] => USB rocket launcher (Gray) 
        [customerid] => 10 ) 
    [2] => Array 
        ( [orderId] => 1544100294 
        [total] => 1614.60 
        [receivedate] => 0000-00-00 
        [stockitemname] => Large sized bubblewrap roll 50m 
        [customerid] => 10 ) )

The array is created from the result of this query:

SELECT oc.orderId, SUM(so.quantity*si.RecommendedRetailPrice) total, oc.receivedate, si.stockitemname, ru.customerid
FROM orderbycustomers oc 
JOIN registered_users ru 
ON oc.customerid = ru.customerid 
JOIN stockitemorders so 
ON oc.orderId = so.orderId 
JOIN stockitems si 
ON so.StockItemID = si.StockItemID
WHERE oc.customerid = $customerid
GROUP BY si.stockitemname
ORDER BY oc.receivedate
1
  • What creates this array? Commented Dec 6, 2018 at 19:20

2 Answers 2

3

Another way in which you can get the stockitems with same id in a unique array.

<?php

 $input = Array ( Array 
       ('orderId' => '1544100294', 
        'total' => 215.28, 
        'receivedate' => '0000-00-00', 
        'stockitemname' => 'The Gu red shirt XML tag t-shirt (White) XXS', 
        'customerid' => 10 ), 
    Array 
       ( 'orderId' => '1544119651', 
        'total' => 37.38, 
        'receivedate' => '0000-00-00', 
        'stockitemname' => 'USB rocket launcher (Gray)', 
        'customerid' => 10 ),
    Array 
        ( 'orderId' => '1544100294', 
        'total' => 1614.60, 
        'receivedate' => '0000-00-00', 
        'stockitemname' => 'Large sized bubblewrap roll 50m', 
        'customerid' => 10 ) );

 // New array using orderId as the key
 $output = array();

 foreach ($input as $values) {
    // Define your key
    $key = $values['orderId'];
    // Assign to the new array using all of the actual values
    $output[$key][] = $values;
 }

 // Get all values inside the array, but without orderId in the keys:
 $output = array_values($output);

 // Print the new array
 print_r($output);
?>

Here you can see it working !

Sign up to request clarification or add additional context in comments.

Comments

1

One way to collect stockitems in order could be like this:

<?php
 $result = [];
 foreach ($ordersFromQuery as $order) {
   $result[$order['orderId']][] = $order;
 }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.