elize
elize

Reputation: 425

How can I merge two JSON strings in PHP?

I have two JSON strings like these:

 $json1 = '[ {"src":"1","order":"2"}, {"src":"10","order":"20"}, ... ]';

and

$json2 = '[ {"src":"4","order":"5"}, {"src":"6","order":"7"}, ... ]';

I am trying to use this to merge them:

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

$json = '[';
    $comma = null;
    foreach($images as $image)
    {
        $comma = ',';
        $json .=      $comma.'{"src":"'.$image['src'].'","order":"'.$image['order'].'"}';
    }
    $json .= ']';
    echo $json;

but I am getting this error:

ERROR: can not use object type of stdCLASS..

What am I doing wrong?

Upvotes: 3

Views: 9812

Answers (4)

Chin Leung
Chin Leung

Reputation: 14921

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

Upvotes: 9

cpugourou
cpugourou

Reputation: 781

if you absolutely want to use it as an array, just set it as one . These can be very useful to set object properties in loops, especially in classes. No your case however.

$json1 = '[ {"src":"1","order":"2"}, {"src":"10","order":"20"}]';
$json2 = '[ {"src":"4","order":"5"}, {"src":"6","order":"7"}]';
$images =  array_merge(json_decode($json1),json_decode($json2));    
$json = '[';
    $comma = null;
    foreach($images as $image)
    {
        $image=(array)$image;
        $comma = ',';
        $json .=      $comma.'{"src":"'.$image['src'].'","order":"'.$image['order'].'"}';
    }
    $json .= ']';

Upvotes: -1

Don't Panic
Don't Panic

Reputation: 41810

The problem is that you are manually doing what json_encode should be doing. This part:

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

is fine.

The individual json_decodes inside the array_merge will decode into arrays of objects, and array_merge will merge them together. You don't need to decode them into multidimensional arrays for this to work.

To get it back into JSON, you should not be using the foreach loop and manually constructing the JSON. The error you are getting is because you are accessing an object using array syntax, but you can avoid the whole problem by just replacing your foreach loop with this:

$json = json_encode($images);

In fact, the whole thing could be done in one line:

$json = json_encode(array_merge(json_decode($json1), json_decode($json2)));

Upvotes: 0

When you make

foreach($images as $image)
{
    $comma = ',';
    $json .=      $comma.'{"src":"'.$image['src'].'","order":"'.$image['order'].'"}';
}

You need change for:

foreach($images as $image)
{
    $json .=      $comma.'{"src":"'.$image->src.'","order":"'.$image->order.'"}';
    $comma = ',';
}

To access the fields of a object you need to use the "->" operator, like $image->src.
Also, the first $comma needs to be null, for this reason I change the order of lines inside the foreach.

Upvotes: 0

Related Questions