PHP array_merge() function

Definition

The array_merge() function combines two or more arrays. array_merge() renumbers numerical indexes and merges string indexes.

Syntax

PHP array_merge() function has the following syntax.

array array_merge ( array arr1 [, array arr2 [, array ...]] )

Parameter

ParameterIs RequiredDescription
array1Required.Array to merge
array2Optional.Array to merge
array3,...Optional.Array to merge

Note

If you want to join two or more arrays together to produce one big array, you need the array_merge() function.

array_merge() joins the array elements of the arrays together to produce the final array. array_push(), array_unshift(), and the square bracket syntax all insert array arguments to produce multidimensional arrays:


<?PHP//from  www .j  a v  a2  s .c om
$authors = array( "Java", "PHP" ); 
$moreAuthors = array( "CSS", "MySQL" ); 
array_push( $authors,  $moreAuthors ); 
print_r( $authors );   
?>

The code above generates the following result.

array_merge() preserves the keys of associative arrays, so we can use it to add new key/value pairs to an associative array:


<?PHP//  www  .  ja va 2s  . c  o  m
    $myBook = array( "title" =>  "Learn PHP from java2s.com", 
                     "author" =>  "java2s.com", 
                     "pubYear" =>  2000 ); 

    $myBook = array_merge( $myBook, array( "numPages" =>  464 ) ); 

    print_r ( $myBook );   
?>

The code above generates the following result.

Example 1

If you add a key/value pair using a string key existing in the array, the original element gets overwritten. This makes array_merge() useful for updating associative arrays:


<?PHP/*w ww  . j a  va 2 s.co  m*/
    $myBook = array( "title" =>  "Learn PHP from java2s.com", 
                     "author" =>  "java2s.com", 
                     "pubYear" =>  2000 ); 

    $myBook = array_merge( $myBook, array( "title" =>  "java from java2s.com", "pubYear" =>  1952 ) ); 
    print_r ( $myBook );   
?>

The code above generates the following result.

Example 2

An element with the same numeric key doesn't get overwritten; instead the new element is added to the end of the array and given a new index:


<?PHP/*ww w . j  av  a2s . c  o m*/
         $authors = array( "Java", "PHP", "CSS", "HTML" ); 
         $authors = array_merge( $authors, array( 0 =>  "MySQL" ) ); 

         print_r ( $authors );   
?>

The code above generates the following result.

Example 3

Use array_merge() to reindex an indexed array by passing the array as the parameter. This ensures that all the elements of an indexed array are consecutively indexed:


<?PHP
$authors = array( 4 =>  "Java", 2 =>  "PHP", 5 =>  "CSS", 7 =>  "HTML" ); 
print_r( array_merge( $authors ) );    
?>

The code above generates the following result.

Example 4

Merge associate array


<?php/*from  w ww  .j  a  va  2  s.c o m*/
$toppings1 = array("A", "B", "C", "D");
$toppings2 = array("D", "java2s.com", "PHP");
$both_toppings = array_merge($toppings1, $toppings2);

var_dump($both_toppings);
?>

The code above generates the following result.

Example 5

The + operator in PHP is overloaded so that you can use it to merge arrays, e.g., $array3 = $array1 + $array2. If PHP finds any keys in the second array that clash with the keys in the first array, they will be skipped.


<?PHP//www .java 2  s  . co  m
$arr1 = array("A"=>1, "B"=>2, "C"=>3);
$arr2 = array("B"=>3, "C"=>8);
$arr3 = $arr1 + $arr2;
var_dump(array_merge($arr1, $arr2));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions