-3

i want to merge this two arrays together

Array
(
    [0] => Array
        (
            [type] => Person
            [relevance] => 0.700000
            [count] => 300
            [text] => Chris
        )
)
Array
(
    [0] => Array
        (
            [type] => Person
            [relevance] => 0.900000
            [count] => 400
            [text] => Chris
        )

    [1] => Array
        (
            [type] => Person
            [relevance] => 0.500000
            [count] => 200
            [text] => Tom
        )
)

so i might have a result like this

Array
(
    [0] => Array
        (
            [type] => Person
            [relevance] => 0.900000
            [count] => 400
            [text] => Chris
        )

    [1] => Array
        (
            [type] => Person
            [relevance] => 0.500000
            [count] => 200
            [text] => Tom
        )
    [2] => Array
        (
            [type] => Person
            [relevance] => 0.700000
            [count] => 300
            [text] => taye
        )
)
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
  • [`array_merge`](http://php.net/array_merge)? – deceze Nov 12 '13 at 11:38
  • please kindly look at the desired result. i want the first array layer to keep its key while the others should continue from the last key of the first array layer. like 0, 1, 2 not 0, 0, 1 i'm talking about the keys. thanks – Theophilus Abiola Alamu Nov 12 '13 at 11:56
  • also assume that this array results are coming from the database and it is assigned to a variable so if i perform the loop test how can i merge it to get the desire result above – Theophilus Abiola Alamu Nov 12 '13 at 11:58
  • Now you've just made it more complicated instead of clarifying it. `array_merge` is perfectly suited for what you want to do. *Unless* you literally mean that array keys need to "continue after the last key of the first array". What if the first array has the keys `2`, `3`? – deceze Nov 12 '13 at 11:59
  • deceze you just got the question. – Theophilus Abiola Alamu Nov 12 '13 at 12:04
  • Why didn't you write that *in the question?!* – deceze Nov 12 '13 at 12:08

4 Answers4

1

Like this?

$array_final = array_merge($array_1, $array_2);

refer array_merge at official documentation site

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • array_merge works if you know the number of the variables to merge but lets assume you getting the multidimensional array result from the database you know there won't be a specific amount or number to merge how will you now merge the them together with you not having a clue of the number of the multidimensional array result you will get. thanks – Theophilus Abiola Alamu Nov 12 '13 at 12:02
  • If every array is in an array for example, you can use like this: `$array_final=call_user_func_array('array_merge', $array_of_arrays);` Or if you are fetching them row by row, then you can append them one-by-one like this: `$result_array=array_merge($result_array,$new_array);` – Lajos Veres Nov 12 '13 at 12:19
  • Lajos Veres you are the man. just figured it with the example you gave. thank you so much. +1 for you men. just a simple line just nearly made Olympus to fall. – Theophilus Abiola Alamu Nov 12 '13 at 13:07
0

Try using foreach

// $array1 is your original array
foreach($array2 as $val) {
     array_push($array2,$val)
}
asiviero
  • 1,225
  • 10
  • 16
0

Use array_merge docs and is faster to check documentation:D

litechip
  • 326
  • 2
  • 9
0

Did you search the PHP Doc ?

what about array_merge_recursive ?

Adrien M.
  • 176
  • 1
  • 6