簡體   English   中英

我如何比較和合並兩個數組

[英]How can i compare and merge two array

我有以下兩個來自同一數據庫的數組,它們使用相同的查詢來獲取兩個數組。 兩者都包含有關具有兩個不同行項目(物料清單)的單個銷售訂單的信息,第一個包含行項目ItemID= 600271 ,第二個包含LineItem ItemID=600274但是正如您在下面看到的,它們都具有相同的銷售訂單號[CustomerSONo] => [7] => **15020**

因此,我如何比較或合並這兩個數組。

//查詢

    $result= --select statement--;
    while($row = mysqli_fetch_array($result)) {
        print_r($row);
     }

陣列1

     Array ( 
           [0] => XXX001 
           [CustomerId] => XXX001 
           [1] => XXX Company Name.*
           [Customer_Bill_Name] => XXX Company Name.* 
           [2] => DHE 
           [WhichShipVia] => DHE [3] => 
           [INV_POSOOrderNumber] => [4] => 2014-12-19 
           [ShipByDate] => 2014-12-19 [5] => 
           [GoodThruDate] => [6] => 
           [CustomerSONo] => [7] => 15020 
           [Reference] => 15020 [8] => 2014-11-25 
           [TransactionDate] => 2014-11-25 [9] => 1 
           [DistNumber] => 1 
           [10] => 70.0000000000000000000 //here is the difference 1
           [Quantity] => 70.0000000000000000000  //here is the difference 2
           [11] => 600271  //here is the difference 3
           [ItemId] => 600271 //here is the difference 4
           [12] => ASSY7.60-15SL/8 FRM I1 15X6 6-6, BLK (GWT-761508 (24) //here is the difference 5
           [SalesDescription] => ASSY7.60-15SL/8 FRM I1 15X6 6-6, BLK (GWT-761508)(24)//here is the difference 1 //here is the difference 6
           [13] => AS1577 //here is the difference 7
           [PartNumber] => AS1577 //here is the difference 8
           [14] => ASSY7.60-15 8PLY W/WHL15X6 BLK //here is the difference 9
           [ItemDescription] => ASSY7.60-15 8PLY W/WHL15X6 BLK )

陣列2:

     Array ( 
            [0] => XXX001 
            [CustomerId] => XXX001 
            [1] => XXX Company Name.*
            [Customer_Bill_Name] => XXX Company Name.* 
            [2] => DHE [WhichShipVia] => DHE [3] =>
            [INV_POSOOrderNumber] =>   [4] => 2014-12-19 
            [ShipByDate] => 2014-12-19  [5] => 
            [GoodThruDate] => [6] => 
            [CustomerSONo] => [7] => 15020 
            [Reference] => 15020 [8] => 2014-11-25 
            [TransactionDate] => 2014-11-25 [9] => 2 
            [DistNumber] => 2 
            [10] => 6.0000000000000000000 //here is the difference 1
            [Quantity] => 6.0000000000000000000 //here is the difference 2
            [11] => 600274  //here is the difference 3
            [ItemId] => 600274 //here is the difference 4
            [12] => ASSY9.5L-15SL/8 FLT I1 15X8 6-6, BLK (GWT-951508)(16)      
            [SalesDescription] => ASSY9.5L-15SL/8 FLT I1 15X8 6-6, BLK (GWT-951508)(16) //here is the difference 5
            [13] => AS1601 //here is the difference 6
            [PartNumber] => AS1601 //here is the difference 7
            [14] => ASSY9.5L-15 W/WHL15X8 6/6 BLK //here is the difference 8
            [ItemDescription] => ASSY9.5L-15 W/WHL15X8 6/6 BLK ) //here is the difference 9

選項1

$orders = array();

while($row = mysqli_fetch_array($result)) {
    $orders[$row['CustomerSONo'][] = $row;
}

這會將共享同一CustomerSONo的所有行一起添加到一個數組中。 如果您有多個訂單,則可以使用來獲取所有單獨的訂單

foreach($orders as $orderNo => $order) {
    foreach($order as $key => $orderRow) {

    }
}

選項2

但是,如果您只想從每個訂單中提取ItemID,則可以執行以下操作:

$orderItems = array();
while($row = mysqli_fetch_array($result)) {
    $orderItems[$row['CustomerSONo']][] = $row['ItemID'];
}

這將創建一個名為$orderItems的數組,該數組僅將訂單號存儲在該數組中,而不是有關該訂單的所有信息。

選項3

如果要回顯這些行,則首先必須像選項2一樣對它們進行“排序”。一旦獲得了所有屬於一個CustomerSONo ItemID ,就可以執行另一個foreach循環以將它們回顯為一行。

foreach($orders as $orderNo => $itemIds) {
    echo "Order #" . $orderNo . ": " . implode(", ", $itemIds);
}

這將創建以下內容:

Order #1: 18340, 1837, 13 Order #2: 183, 868, 285, 860

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM