<?php
$array = [
['brand' => 'ABC', 'model' => 'xyz', 'size' => 13],
['brand' => 'QWE', 'model' => 'poi', 'size' => 23],
['brand' => 'ABC', 'model' => 'xyz', 'size' => 18]
];
var_export(
array_values(
array_reduce(
$array,
function ($carry, $row) {
$compositeKey = $row['brand'] . '_' . $row['model'];
if (!isset($carry[$compositeKey])) {
$carry[$compositeKey] = $row;
} else {
$carry[$compositeKey]['size'] = array_merge(
(array)$carry[$compositeKey]['size'],
[$row['size']]
);
}
return $carry;
}
)
)
);
preferences:
91.34 ms | 411 KiB | 6 Q