我正在使用两个数据数组。
我需要在履行详细信息中找到他们购买的商品并从中获取价值,以便我可以将其与订单详细信息一起存储。
这方面的一个重要方面是重复。订单明细记录不能多次使用相同的订单商品(pinID)。
我们假设这两个对象的大小相同,并包含匹配的相应数据。
示例:
数组1 - 订单详情
stdClass Object
(
[customerID] => 8
[customerProductID] => 118
[productID] => 2
[faceValue] => 25
)
stdClass Object
(
[customerID] => 15
[customerProductID] => 119
[productID] => 2
[faceValue] => 25
)
stdClass Object
(
[customerID] => 14
[customerProductID] => 120
[productID] => 7
[faceValue] => 50
)
stdClass Object
(
[customerID] => 18
[customerProductID] => 121
[productID] => 7
[faceValue] => 50
)
数组2 - 订单履行详情
stdClass Object
(
[pinID] => 75
[denomination] => 25
)
stdClass Object
(
[pinID] => 76
[denomination] => 25
)
stdClass Object
(
[pinID] => 77
[denomination] => 50
)
stdClass Object
(
[pinID] => 78
[denomination] => 50
)
伪代码:
// New object to hold all the updated data
$newObj = [];
// Loop over the order details
foreach($orderDetails->result() as $o){
// Find a pin in the fulfilment details that matches what we need
$newObj[] = $o;
$newObj['pinID'] = get_pin_from_inventory($fulfilmentDetails, $o->faceValue)
}
// Find a matching item in the fulfillment details
function get_pin_from_inventory($fulfilmentDetails, $orderItem){
// Loop over each fulfilment item
foreach($pins->result() as $p){
// If we found a pin that matches our item, return it.
// It is important that this pinID has not already been assigned to another order in the $newObj['pinID']
if($p->denomination == $orderItem){
return $p->pinID;
}
}
}
$ newObj的最终结果
stdClass Object
(
[customerID] => 8
[customerProductID] => 118
[productID] => 2
[faceValue] => 25
[pinID] => 75
)
stdClass Object
(
[customerID] => 15
[customerProductID] => 119
[productID] => 2
[faceValue] => 25
[pinID] => 76
)
stdClass Object
(
[customerID] => 14
[customerProductID] => 120
[productID] => 7
[faceValue] => 50
[pinID] => 77
)
stdClass Object
(
[customerID] => 18
[customerProductID] => 121
[productID] => 7
[faceValue] => 50
[pinID] => 78
)
在创建新对象时,我应该如何检查复制?我是否应该每次从履行阵列中移除物品以防止抓住使用中的物品或者是否有更好的方法?
答案 0 :(得分:1)
这个循环似乎不正确:
// Loop over the order details
foreach($orderDetails->result() as $o){
// Find a pin in the fulfilment details that matches what we need
$newObj[] = $o;
$newObj['pinID'] = get_pin_from_inventory($fulfilmentDetails, $o->faceValue)
}
您将$o
添加到$newObj
array
,然后将新项添加到名为pinID的数组中,而不是将pinID添加到对象中。你应该像这样解决它:
// Loop over the order details
foreach($orderDetails->result() as $o){
// Find a pin in the fulfilment details that matches what we need
$o['pinID'] = get_pin_from_inventory($fulfilmentDetails, $o->faceValue);
$newObj[] = $o;
}
现在,进一步的步骤是确保它不重复:
// Loop over the order details
foreach($orderDetails->result() as $o){
$found = false;
$pinID = get_pin_from_inventory($fulfilmentDetails, $o->faceValue);
foreach ($newObj as $temp) {
//If it is a duplicate, then $found = true
//To check whether it is a duplicate compare its pinID to $pinID
//and do any further checks if needed
}
if (!$found) {
// Find a pin in the fulfilment details that matches what we need
$o['pinID'] = $pinID;
$newObj[] = $o;
}
}