Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two json in PHP

Tags:

json

merge

php

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.

like image 825
Calipso Avatar asked Nov 29 '13 12:11

Calipso


People also ask

What does Json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

How do I add two JSON strings in Java?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.


3 Answers

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

like image 139
DarkSide Avatar answered Oct 09 '22 20:10

DarkSide


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"}]
like image 33
Ben Fortune Avatar answered Oct 09 '22 21:10

Ben Fortune


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)));
like image 22
bobthenob Avatar answered Oct 09 '22 21:10

bobthenob