PHP array_merge() function

In this chapter you will learn:

  1. Definition for PHP array_merge() function
  2. Syntax for PHP array_merge() function
  3. Parameter for PHP array_merge() function
  4. Note for PHP array_merge() function
  5. Example - Update associate arrays
  6. Example - Indexed key got reindexed
  7. Example - Use array_merge() to reindex an indexed array
  8. Example - merge associate array
  9. Example - + vs array_merge()

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/* j av a 2 s.co  m*/
$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//from j  a  v a 2 s . 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/*from  j a va 2s.  c  o  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/*j a  v a 2  s.  co  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/*  ja v  a2 s. c  om*/
$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//from j a  v  a  2 s.com
$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.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_merge_recursive() Function
  2. Syntax for PHP array_merge_recursive() Function
  3. Parameter for PHP array_merge_recursive() Function
  4. Example - Merge two array recursively
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function