Skip to content
Advertisement

Merging two json in PHP

I have two json’s

First one is

JavaScript

Second one is

JavaScript

I want to merge them and have a json like

JavaScript

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

thanks.

Advertisement

Answer

Something like json_encode(array_merge(json_decode($a, true),json_decode($b, true))) should work.

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

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement