The basic expression for the PHP array_merge function would be like the below.

<?php

  $a = array( "value 1", "value 2" );
  $b = array( "value 3", "value 4" );
  
  // Merge the all arrays into one array
  $one_array = array_merge( $a, $b );
  
  print_r( $one_array );
?>

The output:

Array ( 
 [0] => value 1 
 [1] => value 2 
 [2] => value 3 
 [3] => value 4 
)

The PHP array_merge is a built-in PHP function which used to collect and merge two or more arrays together, then return them into one single array.

PHP Array Merge Parameters and the Function Return

The Parameters:

As mentioned before, this built-in function used to merge many arrays together. The PHP array_merge accepts unlimited arguments which would be in array data type.

The array structure would be separated with commas and it can be associative array or whatever array type.

Returns:

The array_merge returns one single array contains all values and keys of the passed old arrays. But here there are some cases. Let’s move to the following section.

How the PHP array_merge() Works

The passed arrays may include an indexed array, associative array, or multidimensional array. All of these array types can be merged together when we using the array_merge. But the array_merge only deleting the old indexes of these arrays if the array type is indexed array.

So If one of the passed arrays is associative array, Indexed array or another type else. It will merge the all types according to its keys except the indexed array. Because the new array will reset a new index for the new generated array. Let’s move to the examples .

PHP array_merge() Examples

Using the array_merge to merge associative array with an indexed array.

<?php

  $indexed = array(12, 25, 68, 91);
  $assoc   = array(
     "name" => "CodedTag",
     "description" => "Free Tutorials and Online Compilers"
  );
  
  $single_array = array_merge( $assoc, $indexed );
  print_r($single_array);
?>

The output:

 Array (
    [name]        => CodedTag
    [description] => Free Tutorials and Online Compilers
    [0] => 12
    [1] => 25
    [2] => 68
    [3] => 91
  )

The new array is already stored the first associative array with its keys and added the second indexed array with new indexes started from “[0]“ and ended with “[3]“.

To ensure from that, in the following example I will add another indexed array before the associative array.

<?php
  $new_index = array("item 1", "item 2");
  $indexed = array(12, 25, 68, 91);
  $assoc   = array(
     "name" => "CodedTag",
     "description" => "Free Tutorials and Online Compilers"
  );
  
  $single_array = array_merge( $new_index, $assoc, $indexed );
  print_r($single_array);
?>

The output:

  Array (
    [0] => item 1
    [1] => item 2
    [name] => CodedTag
    [description] => Free Tutorials and Online Compilers
    [2] => 12
    [3] => 25
    [4] => 68
    [5] => 91
  )

So, there is a new index already assigned with the element keys started from “[0]“ and ended with “[5]“ according to the number of the elements that already assigned from the old indexed arrays.
It ignored the associative arrays by putting it with its keys as in the same old array.

You can also merge multidimensional arrays with any type else.

<?php
  $dimen = array(
    array(
       "Value 1", 
       "Value 2",
       "array" => array(
          1, 2, 3, 4,
       )
    ),
  );
  
  $integer_array = array(1, 2, 3, 4, 5);
  
  $new_array = array_merge( $dimen, $integer_array ); 
  print_r($new_array);
?>

The output:

  Array (
    [0] => Array
        (
            [0] => Value 1
            [1] => Value 2
            [array] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

        )

    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
  )

In the following example, I will merge three arrays and convert it to only single array.

<?php
 $int    = array(1,2,3,4);
 $string = array("Montasser", "Peter", "Jasmine");
 $bool   = array( true, false );
 
 $array = array_merge( $int, $string, $bool ); 
 print_r($array);
?>

The output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => Montasser
    [5] => Peter
    [6] => Jasmine
    [7] => 1
    [8] => 
)

Wrapping Up

The PHP array_merge is a built-in function used to merge many arrays together and then generates a new array with new serials according to the passed arrays.

The array_merge only accepts array data type as arguments.