0

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.

3
  • php.net/manual/fr/function.array-merge.php Commented Apr 25, 2014 at 9:45
  • 1
    Plz post valid php code first. Commented Apr 25, 2014 at 9:49
  • Is this php array? i think you post a json format data. Please post your array data in valid php format. Commented Apr 25, 2014 at 9:52

3 Answers 3

4

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)));
Sign up to request clarification or add additional context in comments.

2 Comments

json_decode() expects parameter 1 to be string, array given
Then just do $array1combinedwitharray2 = array_merge($array1,$array2); echo json_encode($array1combinedwitharray2); instead of the above code.
0

Use the array_merge method : http://www.php.net/manual/en/function.array-merge.php

Comments

0

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)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.