$arr1 = array(
[0] => Array
(
[article] => А6-01-05
[name] => Example
[price] => 9 700
[quantity] => 3
[brand] => Oktan
[code] => УТ-00024246
[city] => Nur-Sultan
)
)
$arr2 = array(
[0] => Array
(
[article] => А6-01-05
[name] => Example
[price] => 9 700
[quantity] => 5
[brand] => Oktan
[code] => УТ-00024246
[city] => Almaty
)
)
$arr = array(
[0] => Array
(
[article] => А6-01-05
[name] => Example
[price] => 9 700
[quantity] =>
[0] => 3
[1] => 5
[brand] => Oktan
[code] => УТ-00024246
[city] =>
[0] => Nur-Sultan
[1] => Almaty
)
)
function merge($idKey, $mergeKeys, ...$data) {
$merged = [];
foreach (array_merge(...$data) as $item) {
$id = $item[$idKey];
if (!array_key_exists($id, $merged)) {
$merged[$id] = [
'unique' => true,
'item' => $item,
];
} else {
if ($merged[$id]['unique']) {
$merged[$id]['unique'] = false;
foreach ($mergeKeys as $k) {
$merged[$id]['item'][$k] = [ $merged[$id]['item'][$k] ];
}
}
foreach ($mergeKeys as $k) {
$merged[$id]['item'][$k][] = $item[$k];
}
}
}
return array_column($merged, 'item');
}
$merged = merge('code', [ 'quantity', 'city' ], $arr1, $arr2);