2

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 :)

3
  • Release year doesn't match on given objects. Is this correct? If so, does this mean you always have to take higher release year value on matching titles when doing merge? Commented Oct 25, 2015 at 14:19
  • I'm confused, you don't want to overwrite data, you want to merge, but the result you're asking for is clearly overwriting, not just merging ? Commented Oct 25, 2015 at 14:23
  • Overwritten: [{ "title": "yy", "releaseYear": "2017" }] Since there is only one object, it overwrites, and merges the second data alone. This is what I meant as overwriting. Commented Oct 25, 2015 at 14:29

1 Answer 1

1

Many possible solutions. One example:

<?php
$data = json_decode('[{"title": "xx","releaseYear": "2014"},{"title": "yy","releaseYear": "2015"}]', true);
$data2 = json_decode('[{"title": "yy","releaseYear": "2017"}]', true);

$data = foo($data, 'title');
$data = foo($data2, 'title', $data);
$data = array_values($data);
var_export($data);

function foo($src, $prop, $target=array()) {
    foreach($src as $o) {
        $target[ $o[$prop] ] = $o;
    }
    return $target;
}
Sign up to request clarification or add additional context in comments.

3 Comments

First of all, thanks for your answer. Well, I ran it, and it gave me this output: array ( 0 => array ( 'title' => 'xx', 'releaseYear' => '2014', ), 1 => array ( 'title' => 'yy', 'releaseYear' => '2017', ), ) That's what I need. But can I get it in JSON format? Like: [{ "title": "xx", "releaseYear": "2014" }, { "title": "yy", "releaseYear": "2017" }] Later, this data will be parsed in Android with Volley, it doesn't recognize that type of data, that's why. Thanks in advance :)
Thanks for the solution @VolkerK. A simple google helped me to get it in JSON format. Used echo json_encode($data); instead of var_export($data); Thanks again, if you're free, do tell me the use of using var_export :)
I just like the output better than that of var_dump ;-) You can copy the output of var_export and paste it (as-is) back into a php script to use it as sample data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.