Php 如何比较和合并两个数组
我有以下两个来自同一数据库的数组,它们使用相同的查询来获取这两个数组。两者都包含关于具有两个不同行项目(物料清单)的单个销售订单的信息。第一个具有行项目Php 如何比较和合并两个数组,php,arrays,Php,Arrays,我有以下两个来自同一数据库的数组,它们使用相同的查询来获取这两个数组。两者都包含关于具有两个不同行项目(物料清单)的单个销售订单的信息。第一个具有行项目ItemID=600271,第二个具有行项目ItemID=600274,但正如您在下面看到的,它们都具有相同的销售订单编号[CustomerSONo]=>[7]=>**15020** 那么如何比较或合并这两个数组呢 //质疑 $result= --select statement--; while($row = mysqli_fe
ItemID=600271
,第二个具有行项目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
这将把共享相同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中那样对它们进行“排序”。一旦您获得了属于一个客户sono
的所有ItemID
,您就可以执行另一个foreach循环,将它们回送到一行中
foreach($orders as $orderNo => $itemIds) {
echo "Order #" . $orderNo . ": " . implode(", ", $itemIds);
}
这将创建以下内容:
$orderItems = array();
while($row = mysqli_fetch_array($result)) {
$orderItems[$row['CustomerSONo']][] = $row['ItemID'];
}
Order#1:183401837,13
Order#2:183868285860
首先定义工会的样子。我不知道你希望最终结果是什么;)这是我发布的一个问题,我自己发布了答案。我发布了真实的链接对不起,请查看。希望如此helps@NielsKeurentjes如何使用for each管理比较他不希望对数组键进行所有比较。他只对CustomerSONo匹配的所有数组感兴趣,所以他可以将这些数组组合在一起。我的方法生成一个数组,其中包含他从MySQL检索到的所有订单,他可以轻松地搜索这些订单。我真的不明白这段代码会有什么问题。使用您在回答他的原始问题时提到的函数,PHP将比较所有索引,而他只希望其中一个匹配。我认为那是不必要的工作。@Niels没问题:-)谢谢Ruben我不懂这行$orders[$value['CustomerSONo'][]=$row;`我如何提取两行项目ItemID=600274
和ItemID=600271
哦,糟糕,我在那里编辑代码时犯了一个错误。$value
应该是$row
。现在,当你从MySQL检索一行时,它将包含一个索引“CustomerSONo”,这是你想要分组的。你知道吗ant只获取项目ID,还是要将订单行分组?在这种情况下,请参阅我回答中的选项2。嗨,鲁本,我想将订单分组并提取两行项目,然后显示在同一行上,如CustomerSONo=15020 | Line item1=600274 | Lineitem2=600271在一行上谢谢!!