So I have 2 json files that I need to merge together, but the situation is somewhat unique.
So lets call the first movies.json:
[
{
"title": "Title of Movie 1",
"description": "description of Movie 1",
"link": "CDN_url_to_movie1",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 2",
"description": "description of Movie 2",
"link": "CDN_url_to_movie2",
"filters": "list, of, filters"
}
]
And lets call the second movies2.json
[
{
"title": "Title of Movie 1",
"description": "description of Movie 1",
"link": "CDN_url_to_movie1"
}
{
"title": "Title of Movie 2",
"description": "description of Movie 2",
"link": "CDN_url_to_movie2",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 3",
"description": "description of Movie 3",
"link": "CDN_url_to_movie3"
}
]
I need to merge these two files in such a way that there are no duplicates while considering filters may not exist in one file or the other.
Thus my desired output from the 2 examples would look like
[
{
"title": "Title of Movie 1",
"description": "description of Movie 1",
"link": "CDN_url_to_movie1",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 2",
"description": "description of Movie 2",
"link": "CDN_url_to_movie2",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 3",
"description": "description of Movie 3",
"link": "CDN_url_to_movie3"
}
]
What I currently have looks like the following
<?php
$arr1 = file_get_contents('movies.json');
$arr2 = json_decode($arr1, true);
$arr3 = file_get_contents('movies2.json');
$arr4 = json_decode($arr3, true);
$arr5 = array_unique(array_merge($arr2, $arr4), SORT_REGULAR);
$arr = json_encode($arr5, JSON_PRETTY_PRINT);
file_put_contents('movies3.json', $arr);
The result of this is:
[
{
"title": "Title of Movie 1",
"description": "description of Movie 1",
"link": "CDN_url_to_movie1",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 2",
"description": "description of Movie 2",
"link": "CDN_url_to_movie2",
"filters": "list, of, filters"
}
{
"title": "Title of Movie 1",
"description": "description of Movie 1",
"link": "CDN_url_to_movie1"
}
{
"title": "Title of Movie 3",
"description": "description of Movie 3",
"link": "CDN_url_to_movie3"
}
]
As we can see the result is not desired. Although it removed the duplicate "movie 2" it considered each "movie 1" unique... I assume because one has the "filters" key and the other does not.
How can I merge these two files in such a way that I will get desired output?