I have two JSON objects, and I want it to be merged into one JSON object. I don't want the JSON data to be overwritten. I want the data to be merged.
Object #1: (example1.json)
[{
"title": "xx",
"releaseYear": "2014",
},
{
"title": "yy",
"releaseYear": "2015"
}]
Object #2: (example2.json)
[{
"title": "yy",
"releaseYear": "2017"
}]
This is the PHP File I currently am using:
<?php
$jsonString = file_get_contents('example.json');
$jsonString2 = file_get_contents('example2.json');
$data = json_decode($jsonString, true);
$data2 = json_decode($jsonString2, true);
$op = array_merge_recursive( $data, $data2 );
$resJson = json_encode($op);
file_put_contents('example3.json', $resJson);
Output received: (example3.json)
[
{
"title":"xx",
"releaseYear":"2014"
},
{
"title":"yy",
"releaseYear":"2015"
},
{
"title":"yy",
"releaseYear":"2017"
}
]
Required Output:
[{
"title": "xx",
"releaseYear": "2014"
},
{
"title": "yy",
"releaseYear": "2017"
}]
Solutions appreciated. Am new to Stackoverflow, so sorry for my bad formatting, if any. Thanks in advance :)