我有2个阵列
$companyA[] = array ("QuantityOnOrder" => $quantityOnOrder, "QuantityOnHand" => $quantityOnHand, "Name" => $name);
$companyB[] = array ("QuantityOnOrder" => $quantityOnOrder, "QuantityOnHand" => $quantityOnHand, "Name" => $name);
这些是多维数组。假设公司A有3条记录而公司B有10条记录。让我们进一步假设公司A中的1条记录与公司B中的记录共享相同的“名称”属性。如果这是真的,那么我想从每个数组中获取quantityOnOrder和quantityOnHand并添加它们的值并将它们存储在一个新数组中。
然后我还要确保这个新数组也具有来自两个数组的所有唯一值。这是一个数组可能看起来像什么以及我希望最终结果看起来像什么的例子。
公司A:
Array (
[0] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 20 [Name] => LOR1)
[1] => Array ([QuantityOnHand] => 5 [QuantityOnOrder] => 6 [Name] => D23-72-P)
[2] => Array ([QuantityOnHand] => 2331 [QuantityOnOrder] => 0 [Name] => RB)
[3] => Array ([QuantityOnHand] => 3520 [QuantityOnOrder] => 0 [Name] => RTOP))
CompanyB:
Array (
[0] => Array ([QuantityOnHand] => 11 [QuantityOnOrder] => 13 [Name] => RPEN)
[1] => Array ([QuantityOnHand] => 5 [QuantityOnOrder] => 6 [Name] => D23-72-P)
[2] => Array ([QuantityOnHand] => 23 [QuantityOnOrder] => 0 [Name] => RAT)
[3] => Array ([QuantityOnHand] => 320 [QuantityOnOrder] => 0 [Name] => RBOT))
CombinedArray:
Array (
[0] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 20 [Name] => LOR1)
[1] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 12 [Name] => D23-72-P)
[2] => Array ([QuantityOnHand] => 2331 [QuantityOnOrder] => 0 [Name] => RB)
[3] => Array ([QuantityOnHand] => 3520 [QuantityOnOrder] => 0 [Name] => RTOP))
[4] => Array ([QuantityOnHand] => 11 [QuantityOnOrder] => 13 [Name] => RPEN)
[5] => Array ([QuantityOnHand] => 23 [QuantityOnOrder] => 0 [Name] => RAT)
[6] => Array ([QuantityOnHand] => 320 [QuantityOnOrder] => 0 [Name] => RBOT))
请注意,在组合数组中,我拥有来自companyA和companyB的所有记录以及共享相同名称的记录。希望这对某人有足够的意义。我无法找到最佳方法来实现我在组合阵列中寻找的结果。任何帮助将不胜感激。
答案 0 :(得分:1)
这是一个你可以使用的概念,你需要让它工作,因为它只是概念(而不是真正的代码)。
$array1 = array();
$array2 = array();
$array3 = array();
$array1.sortByName;
$array2.sortByName;
$array3 = setArray3($array1,$array2,$array3,$array3,0,0);
function setArray3($array1,$array2,$array3,$pointer1,$pointer2){
$count = count($array3);
if(isset($array1[$pointer1])){
if(isset($array2[$pointer2])) {
if ($array1[$pointer1]['name'] > $array2[$pointer2]['name']) {
$array3[$count] = $array1[$pointer1];
$pointer1++;
} elseif ($array1[$pointer1]['name'] < $array2[$pointer2]['name']) {
$array3[$count] = $array2[$pointer2];
$pointer2++;
} else {
//add both the arrays togeather
$row = $array1[$pointer1] + $array2[$pointer2];
$array3[$count] = $row;
$pointer1++;
$pointer2++;
}
setArray3($array1,$array2,$array3,$pointer1,$pointer2);
}else{
// add the rest of array1 to array 3
}
}else{
//add the rest of array 2 to array 3
}
return $array3;
}
答案 1 :(得分:1)
我认为如果这是一个关联数组,关键是名称和数组值,这可能会更容易。
否则,我会首先索引数组以防止更多循环然后必要。这是未经测试的,但也许你可以给这样的东西。
$i = 0;
foreach($company_a as $a) {
$idx_a[$a['Name']] = $i++;
}
$i = 0;
foreach($company_a as $b) {
$idx_b[$b['Name']] = $i++;
}
$toMerge = array_filter($company_b, function($value) {
return array_key_exists($value['Name'], $idx_a);
});
$toRemove = array();
foreach($toMerge as $m) {
$i = $idx_a[$m['Name']];
$c = $company_a[$i];
$company_a[$i]['QuantityOnHand'] = $c['QuantityOnHand'] + $m['QuantityOnHand'];
$company_a[$i]['QuantityOnOrder'] = $c['QuantityOnOrder'] + $m['QuantityOnOrder'];
$toRemove[] = $idx_b[$m['Name']];
}
foreach($toRemove as $i) {
unset($company_b[$]);
}
//company_a now has the final result
$company_a = array_merge($company_a, $company_b);