Reputation: 2963
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.
Upvotes: 0
Views: 182
Reputation: 5627
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)));
Upvotes: 0
Reputation: 68486
Seems like those are JSON data , so decode them using json_decode()
and finally do an array_merge()
with json_encode()
as the wrapper.
$array1combinedwitharray2 = json_encode(array_merge(json_decode($array1,true),json_decode($array2,true)));
Upvotes: 4
Reputation: 348
Use the array_merge method : http://www.php.net/manual/en/function.array-merge.php
Upvotes: 0