首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何比较和合并两个数组

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

Stack Overflow用户
提问于 2014-11-25 22:01:56
回答 1查看 84关注 0票数 0

下面两个来自同一个数据库的数组使用相同的查询来获得这两个数组。它们都包含关于单个销售订单的信息,该订单有两个不同的行项(物料清单),第一个有LineItem ItemID= 600271,第二个有LineItem ItemID=600274,但是如下面所示,它们都有相同的销售订单号[CustomerSONo] => [7] => **15020**

那么,我如何比较或合并这两个数组的。?

//查询

代码语言:javascript
运行
复制
    $result= --select statement--;
    while($row = mysqli_fetch_array($result)) {
        print_r($row);
     }

阵列1

代码语言:javascript
运行
复制
     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:

代码语言:javascript
运行
复制
     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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-25 22:07:54

备选案文1

代码语言:javascript
运行
复制
$orders = array();

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

这将在一个数组中将共享相同CustomerSONo的所有行添加到一起。如果您有多个订单,则可以使用

代码语言:javascript
运行
复制
foreach($orders as $orderNo => $order) {
    foreach($order as $key => $orderRow) {

    }
}

选项2

但是,如果您只想从每个订单中提取ItemID,则可以执行以下操作:

代码语言:javascript
运行
复制
$orderItems = array();
while($row = mysqli_fetch_array($result)) {
    $orderItems[$row['CustomerSONo']][] = $row['ItemID'];
}

这将创建一个名为$orderItems的数组,该数组只存储数组中的序号,而不是有关订单的所有信息。

选项3

如果您想要回显这些行,您首先必须像选项2中那样对它们进行“排序”。一旦您获得了属于一个ItemID的所有CustomerSONo,就需要执行另一个CustomerSONo循环来将它们回显到一行中。

代码语言:javascript
运行
复制
foreach($orders as $orderNo => $itemIds) {
    echo "Order #" . $orderNo . ": " . implode(", ", $itemIds);
}

这将产生以下情况:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27137564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档