Merging Two Json in PHP

Merging two json in PHP

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

How can I merge two JSON strings in PHP?

When you're calling json_decode, you're decoding it as an object. If you want it to be an array, you have to do

$images =  array_merge(json_decode($json1, true), json_decode($json2, true));

For more information about json_decode:

http://php.net/manual/en/function.json-decode.php

Merging two JSON objects with PHP?

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;
}

how to merge two json array or string in php

if your json string have number key:

echo json_encode((object)(array_merge(json_decode('{"1":"A","2":"V","3":"C"}', true), json_decode('{"1":"X","2":"XL","3":"XM","4":"L"}', true))));

This key starts from 0, if you want to start from 1, use array_keys and array_values to get the keys and values. Then use array_map add 1 for each key. At last use array_combine to get the array index from 1.

$array = array_merge(json_decode('{"1":"A","2":"V","3":"C"}', true), json_decode('{"1":"X","2":"XL","3":"XM","4":"L"}', true));
$keys = array_map(function($v){return $v + 1;}, array_keys($array));
$values = array_values($array);
echo json_encode((object)array_combine($keys, $values));

result:

{"1":"A","2":"V","3":"C","4":"X","5":"XL","6":"XM","7":"L"}

if not number key, use array_values before array_merge.

if you want to remove the duplicate, use array_unique after array_merge.

Merge two json array into one

There are some missing pieces there, but I think I see what's happening.

Based on the result you're getting, it looks like $posts and $post_image in this code are eloquent models.

return $this->successResponse($posts.$post_image ); 

When you concatenate them, their __toString() methods convert them to strings, which is done using the toJson() method. So basically you have two JSON objects stuck together, which isn't valid JSON, and then the successResponse() method encodes them again.

To merge them, you can convert them to arrays, merge those, then pass the result to successResponse().

$merged = array_merge($posts->toArray(), $post_image->toArray());
return $this->successResponse($merged);

The result you want is impossible, though. The "post" object has two different values of "id". You'll only be able to get one. If you use

$merged = array_merge($posts->toArray(), $post_image->toArray());

Then the id value of the second object will replace the first one. If you want to keep the first id value, you need to use union instead of array_merge.

$merged = $a->toArray() + $b->toArray(); 


Related Topics



Leave a reply



Submit