I'm trying to merge two Multiple Arrays.
Both arrays have the same item({ "ort": "rom", "lioc": "inrom" }).
$test_1 = '
{
"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" }
]
}
}
';
$test_2 = '
{
"hotels" : {
"hotel" : [
{ "ort": "rom", "lioc": "inrom" },
{ "ort": "beijing", "lioc": "wanda" }
]
}
}
';
$arr_test1 = json_decode($test_1, TRUE);
$arr_test2 = json_decode($test_2, TRUE);
var_dump(json_encode(array_merge_recursive($arr_test1, $arr_test2)));
var_dump(json_encode(array_unique(array_merge($arr_test1,$arr_test2), SORT_REGULAR)));
If I run this code then I get the following results.
result1:
{
"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" },
{"ort":"rom","lioc":"inrom"},
{"ort":"beijing","lioc":"wanda"}
]
}
}
result2:
{
"hotels" : {
"hotel" : [
{ "ort": "rom", "lioc": "inrom" },
{ "ort": "beijing", "lioc": "wanda" }
]
}
}
Both are not my expected results.
As you can see in this screenshot { "ort": "rom", "lioc": "inrom" } item is duplicated in result1.
The point is that don't know what is array's keys are and don't know deep of the array.
I'd like to get the merged array without duplicated items like this.
{
"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" },
{ "ort": "beijing", "lioc": "wanda" }
]
}
}
How can I solve this?
