dqq22391 2014-11-25 22:01
浏览 33
已采纳

我如何比较和合并两个数组

I have following two arrays which comes from the same database use the same query to get both. both contains information about a single sales order having two different line items(Bill of materials) the first one has Line ItemItemID= 600271 and the second one has LineItem ItemID=600274 but as you can see below they both have the same Sales Order Number [CustomerSONo] => [7] => **15020**

so How can i compare or union these two array's. ?

//query

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

Array 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 )

Array 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条回答 默认 最新

  • doushai4890 2014-11-25 22:07
    关注

    Option 1

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

    This will add all rows that share the same CustomerSONo together in one array. If you have multiple orders, you can get all the seperate orders using

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

    Option 2

    If you however only want to extract the ItemIDs from each order, you can do the following:

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

    This will create an array called $orderItems that only stores the order numbers in the array, rather than all the information about the order.

    Option 3

    If you want to echo the lines, you first have to "sort" them like in Option 2. Once you have gained all ItemIDs that belong to one CustomerSONo, you do another foreach loop to echo them into a single line.

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

    This will create the following:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符