<?php
$input1 = ['hotels' => [
'hotel' => [
[ "ort" => "rom", "lioc" => "inrom" ],
[ "ort" => "paris", "lioc" => "inpsrisd" ],
[ "ort" => "berlin", "lioc" => "inberlin" ],
[ "ort" => "milano", "lioc" => "inmilano" ],
[ "ort" => "paris", "lioc" => "anotherinpsrisd"],
[ "ort" => "muc", "lioc" => "inmuc"]
]
]];
$input2 = ['hotels' => [
'hotel' => [
[ "ort" => "rom", "lioc" => "inrom" ],
[ "ort" => "beijing", "lioc" => "wanda" ]
]
]];
function recursiveUnique($input): array
{
// if the input is not an array, just return the input
if (!is_array($input)) {
return $input;
}
// check if the array is a list or an associative array
else if (count($input) === 0 || array_keys($input) === range(0, count($input) - 1)) {
return array_values(array_unique($input, SORT_REGULAR));
}
else {
foreach ($input as $key => $value) {
$input[$key] = recursiveUnique($value);
}
return $input;
}
}
var_dump(recursiveUnique(array_merge_recursive($input1, $input2)));
preferences:
25.14 ms | 409 KiB | 5 Q