Question

array1 look like this

 $array1 = [{
        'id': 1,
            'name': 'John'
    }]

and here is array2 :

$array2 = [{
    'id': 1,
        'name': 'someone'
}, {
    'id': 1,
        'name': 'Rocky'
}, {
    'id': 1,
        'name': 'Samuel'
}]

I want something like this:

$array1combinedwitharray2 = [{
    'id': 1,
        'name': 'John'
}, {
    'id': 1,
        'name': 'someone'
}, {
    'id': 1,
        'name': 'Rocky'
}, {
    'id': 1,
        'name': 'Samuel'
}

]

I tried several time and the result was the array goes into another array.

Was it helpful?

Solution

Seems like those are JSON data , so decode them using json_decode() and finally do an array_merge() with json_encode() as the wrapper.

The code..

$array1combinedwitharray2 = json_encode(array_merge(json_decode($array1,true),json_decode($array2,true)));

OTHER TIPS

Those are JSON Objects first you need to convert them to array then merge them and then encode to JSON format.

$array1combinedwitharray2 = json_encode(array_merge(json_decode($array1,true),json_decode($array2,true)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top