Merging two json in PHP

Asked
Active3 hr before
Viewed126 times

7 Answers

merging
90%

STEP-2) Next we should convert the json strings into an array and join them into a single array.,STEP-1) Let's take two different jsons containing couple of employee details.,You can also store the json output in a file instead of displaying it in the browser window.,STEP-3) Then convert the merged array to json. For this we have to use the function json_encode(). While encoding, I have included the token 'JSON_PRETTY_PRINT' to add proper indentation so that it's easy on the eyes.

STEP-1) Let's take two different jsons containing couple of employee details.

< ? php
$json1 = '{"id": "GD01", "name": "Garrett Davidson", "position": "System Administrator", "location": "New York"}';
$json2 = '{"id": "DW02", "name": "Donna Winters", "position": "Senior Programmer", "location": "Seattle"}'; ?
>
load more v
88%

see my edit above. That should be closer to what you need, but I guess that will return objects as encoded arrays in JSON as result (sorry, no time to test that now) :( – DarkSide Nov 29 '13 at 13:00 ,EDIT 2: try array-merge-recursive and see my comment below. Sorry have to log out now :( This looks like a full correct solution: https://stackoverflow.com/a/20286594/1466341, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Student asked me if it is necessary to simplify fractions at the end of answering a question. I'm not sure how to respond

Managed to throw this together. There is most likely a better solution, but this is the closest I got.

$a = '[{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number"},{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number"}]';
$b = '[{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521"},{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUEE":"C1435"}]';
$r = [];
foreach(json_decode($a, true) as $key => $array) {
   $r[$key] = array_merge(json_decode($b, true)[$key], $array);
}
echo json_encode($r);

returns,

[{
      "COLUMN_NAME": "ORDER_NO",
      "DEFAULT_VALUE": "1521",
      "COLUMN_TITLE": "Order Number"
   },
   {
      "COLUMN_NAME": "CUSTOMER_NO",
      "DEFAULT_VALUEE": "C1435",
      "COLUMN_TITLE": "Customer Number"
   }
]
load more v
72%

Merging one or more JSON arrays using PHP can be done in various ways. For example, the merge can be done by using PHP array_merge() function or by pushing each JSON array into a target array.,In this tutorial, we are going to use one of the easiest methods of merging JSON array using PHP. For that, the input JSON has to be decoded before merging.,You can have a glance with one of my previous articles if you are looking for a beginners guide to learn more on JSON handling with PHP.,PHP ArraysPHP JSON Array MergePHP in_array

This code is used to merge given two JSON objects

<?php
$json1 = '{
	"id": "#001",
    "username": "Tom",
    "type": "admin",
    "status": "active"   
}';

$json2 =  '{
    "id": "#002",
    "username": "Jerry",
    "type": "user",
    "status": "Inactive"
}';
$user[] = json_decode($json1,true);
$user[] = json_decode($json2,true);
$json_merge = json_encode($user);
?>

<h4>Given JSON String:</h4> 
<p>
<div>$json1 = <?php echo $json1; ?></div>
<div>$json2 = <?php echo $json2; ?></div>
</p>
<h4>Output:</h4> 
<div><?php echo $json_merge; ?></div>
65%

Related Search : PHP JSON Array Merge,merge two json objects php,php merge arrays,array merge and combine in php,php add array to array,how to merge json array,merge two array into one array in php,merge two arrays into json php,php array union,How to merge two PHP JSON array into one,There are the Following The simple About Merge Multiple PHP JSON Array Merge into one array Full Information With Example and source code.,I am having two PHP JSON Array Merge objects to merge together Example,Today, We want to share with you Merge Multiple PHP JSON Array Merge into one array.In this post we will show you merge two array into one array in php, hear for merge two array into one array in php(php merge arrays) we will give you demo and example for implement.In this post, we will learn about Merge one or more arrays recursively(Merge one or more arrays) with an example.

<?php
$firstjsondata = '{
	"id": "#007",
    "membername": "Jaydeep",
    "type": "admin",
    "status": "active"   
}';

$secondjsondata =  '{
    "id": "#008",
    "membername": "Krunal",
    "type": "member",
    "status": "Inactive"
}';
$member[] = json_decode($firstjsondata,true);
$member[] = json_decode($secondjsondata,true);
$json_results_merge = json_encode($member);
?>

<h4>Given JSON String:</h4> 
<p>
<div>$firstjsondata = <?php echo $firstjsondata; ?></div>
<div>$secondjsondata = <?php echo $secondjsondata; ?></div>
</p>
<h4>Output:</h4> 
<div><?php echo $json_results_merge; ?></div>
load more v
75%

I need to merge two json file in PHP. One is at first empty, and the other one changes at each call. ,I found a lot of codes to merge two JSON array into one in PHP but it doesn't work for me. ,If you add any files,it will delete all existing files related to this answer-(only this answer),You first need to convert your JSON to arrays before you can do anything with it in PHP.

I found a lot of codes to merge two JSON array into one in PHP but it doesn't work for me.

$final_array = array();
$first_json = file_get_contents("test3.json");
$second_json = file_get_contents("my-file.json");

if (json_decode($first_json, true) == null) {
   $final_array[] = json_decode($second_json, true);
   $merge_final_array = json_encode(json_decode($second_json, true));
} else {
   $final_array[] = json_decode($first_json, true);
   $final_array[] = json_decode($second_json, true);
   $merge_final_array = json_encode($final_array);
}

file_put_contents("test3.json", $merge_final_array);
$merge_final_array = null;

This should give me :

[{
   "score": 15,
   "win": true,
   "device": "Android SDK built for x86"
}, {
   "score": "Finish",
   "device": "Android SDK built for x86",
   "win": true
}, {
   "win": true,
   "score": "Finish",
   "device": "Android SDK built for x86"
}, {
   "score": 16,
   "scenario": "Finish",
   "win": true,
   "device": "Android SDK built for x86"
}]

As it gives me:

[
   [
      [{
         "score": 15,
         "win": true,
         "device": "Android SDK built for x86"
      }, {
         "score": "Finish",
         "device": "Android SDK built for x86",
         "win": true
      }], {
         "win": true,
         "score": "Finish",
         "device": "Android SDK built for x86"
      }
   ], {
      "score": 16,
      "scenario": "Finish",
      "win": true,
      "device": "Android SDK built for x86"
   }
]
load more v
40%

EDIT 2: try array-merge-recursive and see my comment below. Sorry have to log out now :( This looks like a full correct solution: https://stackoverflow.com/a/20286594/1466341,EDIT: try adding true as second parameter to json_decode. That'll convert objects to associative arrays.,array_merge in official PHP documentation,I want to merge them and have a json like

First one is

    [{
       "COLUMN_NAME": "ORDER_NO",
       "COLUMN_TITLE": "Order Number"
    }, {
       "COLUMN_NAME": "CUSTOMER_NO",
       "COLUMN_TITLE": "Customer Number"
    }]

Second one is

[{
      "COLUMN_NAME": "ORDER_NO",
      "DEFAULT_VALUE": "1521"
   },
   {
      "COLUMN_NAME": "CUSTOMER_NO",
      "DEFAULT_VALUEE": "C1435"
   }
]

I want to merge them and have a json like

[{
   "COLUMN_NAME": "ORDER_NO",
   "COLUMN_TITLE": "Order Number",
   "DEFAULT_VALUE": "1521"
}, {
   "COLUMN_NAME": "CUSTOMER_NO",
   "COLUMN_TITLE": "Customer Number",
   "DEFAULT_VALUEE": "C1435"
}]
load more v
22%

< ? php
$json1 = '{"id": "GD01", "name": "Garrett Davidson", "position": "System Administrator", "location": "New York"}';
$json2 = '{"id": "DW02", "name": "Donna Winters", "position": "Senior Programmer", "location": "Seattle"}';

// decode json to array
$array[] = json_decode($json1, true);
$array[] = json_decode($json2, true);

// encode array to json
$result = json_encode($array, JSON_PRETTY_PRINT);

// print merged json
header('Content-type: text/javascript');
echo $result; ?
>

Other "merging-undefined" queries related to "Merging two json in PHP"