[英]group/sum multiple related arrays
4 arrays 給出。 如果 arrays 在不同位置具有相同的值,我想按它的值分組,如果分組,則對 integer 值求和。
$arr1 = ["101","206", "206"];
$arr2 = ["PA701", "PA700", "PA700"];
$arr3 = ["2022-04-01", "2022-04-07", "2022-04-07"];
$arr4 = [70, 1040, 1625];
//expected Result
$res1 = ["101","206"];
$res2 = ["PA701", "PA700"];
$res3 = ["2022-04-01", "2022-04-07"];
$res4 = [70, 2665];
數組 1-3 鏈接在一起,因此僅當它們的每個數組的值在多個位置上相同時才允許分組。
此示例未分組,因為在 $arr2 中我們沒有相關的重復項。
$arr1 = ["101","206", "206"];
$arr2 = ["PA701", "PA701", "PA700"];
$arr3 = ["2022-04-01", "2022-04-07", "2022-04-07"];
$arr4 = [70, 1040, 1625];
//expected Result
$res1 = ["101","206", "206"];
$res2 = ["PA701", "PA701", "PA700"];
$res3 = ["2022-04-01", "2022-04-07", "2022-04-07"];
$res4 = [70, 1040, 1625];
我的嘗試有效,但前提是重復發生在數組的下一個 position 上,我想讓它工作,而不管數組 position。
foreach($arr1 as $k=>$val)
{
if(isset($arr1[$k+1]))
{
if($arr1[$k] == $arr1[$k+1] && $arr2[$k] == $arr2[$k+1] && $arr3[$k] == $arr3[$k+1])
{
unset($arr1[$k+1]);
unset($arr2[$k+1]);
unset($arr3[$k+1]);
$arr4[$k] += $arr4[$k+1];
unset($arr4[$k+1]);
}
}
}
這里的例子
這可能是使用 array_map 和 array_reduce 最簡單的方法。 首先,您將所有這些單獨的 arrays 聚合成一個,每個索引都有特定的條目。 然后,如果我們應該將它添加到分組數組中,或者如果我們已經有一個重復的項目,那么您將對該數組中的每個條目進行推理。
我們顯然沒有內置任何保護措施。例如,我們不檢查 arrays 是否具有相同的長度,或者$arr4
中的項目是否都是數字,或者其他 arrays 中的項目是否可以很容易地與===
相比。
$arr1 = ["101","206", "206"];
$arr2 = ["PA701", "PA700", "PA700"];
$arr3 = ["2022-04-01", "2022-04-07", "2022-04-07"];
$arr4 = [70, 1040, 1625];
$items = array_map(
function ($item1, $item2, $item3, $item4) {
return [
'item1' => $item1,
'item2' => $item2,
'item3' => $item3,
'item4' => $item4
];
},
$arr1,
$arr2,
$arr3,
$arr4
);
// group by item1/2/3
$groupedItems = array_reduce(
$items,
function ($acc, $entry) {
// First we figure out if there is a duplicate in our current reduced array
$duplicateEntryIndex = null;
foreach ($acc as $accIndex => $accEntry) {
if ($accEntry['item1'] === $entry['item1'] && $accEntry['item2'] === $entry['item2'] && $accEntry['item3'] === $entry['item3']) {
$duplicateEntryIndex = $accIndex;
break;
}
}
if (is_null($duplicateEntryIndex)) {
// If we do not have a duplicate entry, we just add the current entry to the new grouped array
$acc[] = $entry;
} else {
// Otherwise we modify the existing entry to be the sum of the two
$acc[$duplicateEntryIndex]['item4'] += $entry['item4'];
}
return $acc;
},
[]
);
var_dump($groupedItems);
如果您不使用 4 個單獨的變量,則可以更優雅地完成此任務。 包含 4 個子數組的多維數組會使編程更容易。
無論如何,不要做一個以上的循環。 您只需要根據 3 個標識列值分配臨時復合鍵,然后在多次遇到匹配組時對第 4 個求和。 如果願意,可以在使用array_values()
完成循環后刪除臨時鍵。
代碼:(演示)
$arr1 = ["101","206", "206"];
$arr2 = ["PA701", "PA700", "PA700"];
$arr3 = ["2022-04-01", "2022-04-07", "2022-04-07"];
$arr4 = [70, 1040, 1625];
foreach ($arr1 as $i => $value) {
$compositeKey = "{$value}_{$arr2[$i]}_{$arr3[$i]}";
if (!isset($res1[$compositeKey])) {
$res1[$compositeKey] = $value;
$res2[$compositeKey] = $arr2[$i];
$res3[$compositeKey] = $arr3[$i];
$res4[$compositeKey] = $arr4[$i];
} else {
$res4[$compositeKey] += $arr4[$i];
}
}
var_export(array_values($res1));
var_export(array_values($res2));
var_export(array_values($res3));
var_export(array_values($res4));
Output:
array (
0 => '101',
1 => '206',
)array (
0 => 'PA701',
1 => 'PA700',
)array (
0 => '2022-04-01',
1 => '2022-04-07',
)array (
0 => 70,
1 => 2665,
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.