<?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)));
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.26, 8.4.1 - 8.4.13
- array(1) {
["hotels"]=>
array(1) {
["hotel"]=>
array(7) {
[0]=>
array(2) {
["ort"]=>
string(3) "rom"
["lioc"]=>
string(5) "inrom"
}
[1]=>
array(2) {
["ort"]=>
string(5) "paris"
["lioc"]=>
string(8) "inpsrisd"
}
[2]=>
array(2) {
["ort"]=>
string(6) "berlin"
["lioc"]=>
string(8) "inberlin"
}
[3]=>
array(2) {
["ort"]=>
string(6) "milano"
["lioc"]=>
string(8) "inmilano"
}
[4]=>
array(2) {
["ort"]=>
string(5) "paris"
["lioc"]=>
string(15) "anotherinpsrisd"
}
[5]=>
array(2) {
["ort"]=>
string(3) "muc"
["lioc"]=>
string(5) "inmuc"
}
[6]=>
array(2) {
["ort"]=>
string(7) "beijing"
["lioc"]=>
string(5) "wanda"
}
}
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(1) {
["hotels"]=>
array(1) {
["hotel"]=>
array(7) {
[0]=>
array(2) {
["ort"]=>
string(3) "rom"
["lioc"]=>
string(5) "inrom"
}
[1]=>
array(2) {
["ort"]=>
string(5) "paris"
["lioc"]=>
string(8) "inpsrisd"
}
[2]=>
array(2) {
["ort"]=>
string(6) "berlin"
["lioc"]=>
string(8) "inberlin"
}
[3]=>
array(2) {
["ort"]=>
string(6) "milano"
["lioc"]=>
string(8) "inmilano"
}
[4]=>
array(2) {
["ort"]=>
string(5) "paris"
["lioc"]=>
string(15) "anotherinpsrisd"
}
[5]=>
array(2) {
["ort"]=>
string(3) "muc"
["lioc"]=>
string(5) "inmuc"
}
[6]=>
array(2) {
["ort"]=>
string(7) "beijing"
["lioc"]=>
string(5) "wanda"
}
}
}
}
preferences:
105.09 ms | 412 KiB | 5 Q