26

I have two json's

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"}]

is there a way to merge them? It is also OK for me if a stucture change in JSON is required

thanks.

Calipso
  • 957
  • 3
  • 15
  • 33

4 Answers4

57

Something like this should work:

json_encode(
    array_merge(
        json_decode($a, true),
        json_decode($b, true)
    )
)

or the same as one-liner:

json_encode(array_merge(json_decode($a, true),json_decode($b, true)))

array_merge in official PHP documentation

json_decode in official PHP documentation

EDIT: try adding true as second parameter to json_decode. That'll convert objects to associative arrays.

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

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • 1
    thank you for the answer but the result is [{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number"},{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number"},{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521"},{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUE":"C1435"}] it is adding the second json after the first one. isn't it possible only to have the missing key and value added to the first one.. Please see my expected result – Calipso Nov 29 '13 at 12:56
  • 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
  • It didn't change the result. Again it is adding the array after the previous one – Calipso Nov 29 '13 at 13:03
  • 1
    Ah I see now. You have to merge elements recursively like with this function http://us3.php.net/manual/en/function.array-merge-recursive.php, but catch here is that when you have numeric keys in array, then it'll not merge sub-elements. Brutal solution would be to first decode all jsons, then merge their sub-elements in loop one by one and then encode result in json. Sorry, have to log out now :( – DarkSide Nov 29 '13 at 13:06
  • Like I said - this looks like a correct solution below: http://stackoverflow.com/a/20286594/1466341 – DarkSide Nov 29 '13 at 13:08
10

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"}]
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
7

This works like a charm for me

json_encode(array_merge(json_decode($a, true),json_decode($b, true)))

here is a full example

$query="SELECT * FROM `customer` where patient_id='1111118'";

$mysql_result = mysql_query($query);

$rows = array();
while($r = mysql_fetch_assoc($mysql_result)) {
    $rows[] = $r;
}
$json_personal_information=json_encode($rows);
//echo $json_personal_information;




$query="SELECT * FROM `doctor` where patient_id='1111118'";

$mysql_result = mysql_query($query);

$rows = array();
while($r = mysql_fetch_assoc($mysql_result)) {
    $rows[] = $r;
}
$json_doctor_information=json_encode($rows);
//echo $json_doctor_information;

echo $merger=json_encode(array_merge(json_decode($json_personal_information, true),json_decode($json_doctor_information, true)));
bobthenob
  • 149
  • 2
  • 2
  • I must be missing something. Won't the 2nd encode write over the first one? – Gary Carlyle Cook Mar 03 '18 at 02:25
  • @GaryCarlyleCook because after each while loop, the result of $rows is stored to a different variable ($json_personal_information and $json_doctor_information). These two variables are used in the array_merge. – Lewis Donovan Oct 04 '18 at 11:38
0

This worked to me!

$dados1 = json_encode(array('data'=>'1')); 
$dados2 = json_encode(array('data2'=>'2')); 
echo json_encode(array_merge(json_decode($dados1, true),json_decode($dados2, true)));
dazed-and-confused
  • 1,293
  • 2
  • 11
  • 19