How can JSON objects be merged in PHP?

To merge JSON objects in PHP, you can decode the JSON strings into associative arrays using json_decode(), merge the arrays using array_merge(), and then encode the merged array back into JSON using json_encode(). This allows you to combine the data from multiple JSON objects into a single JSON object.

$json1 = '{"name": "John", "age": 30}';
$json2 = '{"city": "New York", "country": "USA"}';

$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);

$mergedArray = array_merge($array1, $array2);

$mergedJson = json_encode($mergedArray);

echo $mergedJson;