将php中的多维数组与重复值合并

将php中的多维数组与重复值合并,php,arrays,multidimensional-array,array-merge,Php,Arrays,Multidimensional Array,Array Merge,如您所见,我有一个具有重复orderid的数组 因为一个订单可以有多个stockitems(stockitemname)。如何合并数组,以便合计字段,orderid不再重复,所有StockItemName都放在同一orderid下 有人有办法解决这个问题吗 Array ( [0] => Array ([orderId] => 1544100294 [total] => 215.28 [receivedate] => 00

如您所见,我有一个具有重复orderid的数组 因为一个订单可以有多个stockitems(stockitemname)。如何合并数组,以便合计字段,orderid不再重复,所有StockItemName都放在同一orderid下

有人有办法解决这个问题吗

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 ) )
数组是根据此查询的结果创建的:

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

订单中收集
stockitems
的一种方法如下:

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

订单中收集
库存项目的一种方法可以如下所示:

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

另一种方法,您可以在
唯一数组中获得具有相同
id
stockitems

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


你可以看到它在工作

另一种方法,您可以在
唯一数组中获得具有相同
id
stockitems

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


你可以看到它在工作

是什么创建了这个数组?是什么创建了这个数组?很乐意帮助:)很乐意帮助:)