3

I'm working with PHP 5.6 and I want to push values from an array in the end of another array so I tried the array_push function but It pushed the whole array like this:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
       (
           [0] => d
           [1] => e
           [2] => f
       )
)

What I'm looking for is this :

 Array
 (
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
       )

Is there any simpler way than looping over the array and adding values one by one :)

Prasad
  • 1,562
  • 5
  • 26
  • 40
storm
  • 379
  • 1
  • 9
  • 23

2 Answers2

7
$r = array
(
    array("a","b","c"),
    array("d","e","f")
);
$r1[] = call_user_func_array('array_merge', $r);
print_r($r1);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
6

You can use the array_merge function, which appends together multiple arrays:

<?php
$array1 = array('a','b','c');
$array2 = array('d','e','f');
$array1 = array_merge($array1, $array2);
var_dump($array1); // array('a', 'b', 'c', 'd', 'e', 'f')
ajshort
  • 3,684
  • 5
  • 29
  • 43
Nayeem Sarwar
  • 134
  • 11