<?php
$array = [
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'test1', 'C' => 2, 'count' => 1],
];
$result = [];
foreach ($array as $row) {
$key = implode('~', array_slice($row, 0, 3));
if (!isset($result[$key])) {
$result[$key] = $row;
} else {
$result[$key]['count'] += $row['count'];
}
}
var_export(array_values($result));
- Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- array (
0 =>
array (
'A' => 'O',
'B' => 'O',
'C' => 1,
'count' => 2,
),
1 =>
array (
'A' => 'Test',
'B' => 'Test',
'C' => 1,
'count' => 2,
),
2 =>
array (
'A' => 'Test',
'B' => 'test1',
'C' => 2,
'count' => 1,
),
)
preferences:
120.75 ms | 403 KiB | 122 Q