Array merge multiple arrays php

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. The function takes the list of arrays separated by commas as a parameter that are needed to be merged and returns a new array with merged values of arrays passed in parameter.

    Syntax:

    array array_merge($array1, $array2, ......, $arrayn)

    Parameters: The array_merge() function takes a list of arrays separated by commas as a parameter that are needed to be merged as shown in the syntax. There are n arrays (($array1, $array2, ……, $arrayn) separated by (‘,’) in the syntax. We can pass any number of arrays in parameter.

    Return Value: It returns a new array in which the elements of all arrays passed in parameters are merged such that the values of one array are appended at the end of the previous array.

    Below programs illustrates the working of array_merge() function in PHP:

    • Merging Two Simple Arrays: When two more arrays are passed to the array_merge() function then the values of one array are appended at the end of the previous array. If two elements have the same string keys then the latter value will be overridden. The integer keys will be renumbered starting from zero. To merge two arrays, the array_merge() function can be executed in the following way:

      $my_array1 = array("size" => "big", 2,3 );

      $my_array2 = array("a", "b", "size" => "medium",

                              "shape" => "circle", 4);

      $res = array_merge($my_array1, $my_array2);

      print_r($res);

      ?>

      Output:

      Array
      (
          [size] => medium
          [0] => 2
          [1] => 3
          [2] => a
          [3] => b
          [shape] => circle
          [4] => 4
      )
      

      Note: If the input arrays contain the same string keys, then the later value for that key will overwrite the previous one.

    • Passing parameter with integer keys: If parameters are passed to the array_merge() function and the keys of this array parameter are an integer then the keys in the output array will be renumbered starting from 0 and incrementing for next elements by 1.

      Below programs illustrates this:

      $my_array = array(1 => "Geeks", 3=>"for", 2=>"Geeks");

      $res = array_merge($my_array);

      print_r($res);

      ?>

      Output:

      Array
      (
          [0] => Geeks
          [1] => for
          [2] => Geeks     
      )

      $my_array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');

      $my_array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');

      $res = array_merge($my_array1,$my_array2);

      print_r($res);

      ?>

      Output:

      Array
      (
          [0] => zero_a
          [1] => two_a
          [2] => three_a
          [3] => one_b
          [4] => three_b
          [5] => four_b
      )
      

    ❮ PHP Array Reference

    Example

    Merge two arrays into one array:

    $a1=array("red","green");
    $a2=array("blue","yellow");
    print_r(array_merge($a1,$a2));
    ?>

    Try it Yourself »


    Definition and Usage

    The array_merge() function merges one or more arrays into one array.

    Tip: You can assign one array to the function, or as many as you like.

    Note: If two or more array elements have the same key, the last one overrides the others.

    Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See example below).

    Tip: The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.


    Syntax

    array_merge(array1, array2, array3, ...)

    Parameter Values

    ParameterDescription
    array1 Required. Specifies an array
    array2 Optional. Specifies an array
    array3,... Optional. Specifies an array


    Technical Details

    Return Value:Returns the merged array
    PHP Version:4+
    Changelog:As of PHP 5.0, this function only accept parameters of type array

    More Examples

    Example

    Merge two associative arrays into one array:

    $a1=array("a"=>"red","b"=>"green");
    $a2=array("c"=>"blue","b"=>"yellow");
    print_r(array_merge($a1,$a2));
    ?>

    Try it Yourself »

    Example

    Using only one array parameter with integer keys:

    $a=array(3=>"red",4=>"green");
    print_r(array_merge($a));
    ?>

    Try it Yourself »


    ❮ PHP Array Reference


    Can we merge more than two array in PHP?

    The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.

    How can I merge two arrays in PHP without duplicates?

    You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

    What is the difference between array_merge () and Array_merge_recursive () in PHP?

    The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

    How do you merge arrays?

    To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.