GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • Getting Started
  • Introduction
  • A simple tutorial
  • Language Reference
  • Basic syntax
  • Types
  • Variables
  • Constants
  • Expressions
  • Operators

This view

  • Original page
  • New address

GeezerVison is showing www.php.net — PHP: array_merge - Manual

PHP 8.6.0 RC5 available for testing
    array_merge_recursive »
    « array_map
    • Preface
    • Function Reference
    • Variable and Type Related Extensions
    • Arrays
    • Array Functions

    array_merge

    (PHP 4, PHP 5, PHP 7, PHP 8)

    array_merge — Merge one or more arrays

    Description

    function array_merge(array ...$arrays): array

    Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

    Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

    Parameters

    arrays

    Variable list of arrays to merge.

    Return Values

    Returns the resulting array. If called without any arguments, returns an empty array.

    Changelog

    Version Description
    7.4.0 This function can now be called without any parameter. Formerly, at least one parameter has been required.

    Examples

    Example #1 array_merge() example

    <?php
    
    $array1 = ["color" => "red", 2, 4];
    $array2 = ["a", "b", "color" => "green", "shape" => "trapezoid", 4];
    
    $result = array_merge($array1, $array2);
    
    print_r($result);

    The above example will output:

    Array
    (
        [color] => green
        [0] => 2
        [1] => 4
        [2] => a
        [3] => b
        [shape] => trapezoid
        [4] => 4
    )

    Example #2 Simple array_merge() example

    <?php
    
    $array1 = [];
    $array2 = [1 => "data"];
    
    $result = array_merge($array1, $array2);
    
    print_r($result);

    Don't forget that numeric keys will be renumbered!

    Array
    (
        [0] => data
    )

    If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

    <?php
    
    $array1 = [0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'];
    $array2 = [1 => 'one_b', 3 => 'three_b', 4 => 'four_b'];
    
    $result = $array1 + $array2;
    
    var_dump($result);

    The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

    array(5) {
      [0]=>
      string(6) "zero_a"
      [2]=>
      string(5) "two_a"
      [3]=>
      string(7) "three_a"
      [1]=>
      string(5) "one_b"
      [4]=>
      string(6) "four_b"
    }

    See Also

    • array_merge_recursive() - Merge one or more arrays recursively
    • array_replace() - Replaces elements from passed arrays into the first array
    • array_combine() - Creates an array by using one array for keys and another for its values
    • array operators

    Found A Problem?

    Learn How To Improve This Page • Submit a Pull Request • Report a Bug
    +add a note

    User Contributed Notes 4 notes

    up
    down
    322
    Julian Egelstaff ¶
    17 years ago
    In some situations, the union operator ( + ) might be more useful to you than array_merge.  The array_merge function does not preserve numeric key values.  If you need to preserve the numeric keys, then using + will do that.
    
    ie:
    
    <?php
    
    $array1[0] = "zero";
    $array1[1] = "one";
    
    $array2[1] = "one";
    $array2[2] = "two";
    $array2[3] = "three";
    
    $array3 = $array1 + $array2;
    
    //This will result in::
    
    $array3 = array(0=>"zero", 1=>"one", 2=>"two", 3=>"three");
    
    ?>
    
    Note the implicit "array_unique" that gets applied as well.  In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.
    
    --Julian
    up
    down
    60
    ChrisM ¶
    4 years ago
    I wished to point out that while other comments state that the spread operator should be faster than array_merge, I have actually found the opposite to be true for normal arrays. This is the case in both PHP 7.4 as well as PHP 8.0. The difference should be negligible for most applications, but I wanted to point this out for accuracy.
    
    Below is the code used to test, along with the results:
    
    <?php
    $before = microtime(true);
    
    for ($i=0 ; $i<10000000 ; $i++) {
        $array1 = ['apple','orange','banana'];
        $array2 = ['carrot','lettuce','broccoli'];
        
        $array1 = [...$array1,...$array2];
    }
    
    $after = microtime(true);
    echo ($after-$before) . " sec for spread\n";
    
    $before = microtime(true);
    
    for ($i=0 ; $i<10000000 ; $i++) {
        $array1 = ['apple','orange','banana'];
        $array2 = ['carrot','lettuce','broccoli'];
        
        $array1 = array_merge($array1,$array2);
    }
    
    $after = microtime(true);
    echo ($after-$before) . " sec for array_merge\n";
    ?>
    
    PHP 7.4:
    1.2135608196259 sec for spread
    1.1402177810669 sec for array_merge
    
    PHP 8.0:
    1.1952061653137 sec for spread
    1.099925994873 sec for array_merge
    up
    down
    19
    Andreas Hofmann ¶
    4 years ago
    In addition to the text and Julian Egelstaffs comment regarding to keep the keys preserved with the + operator:
    When they say "input arrays with numeric keys will be renumbered" they MEAN it. If you think you are smart and put your numbered keys into strings, this won't help. Strings which contain an integer will also be renumbered! I fell into this trap while merging two arrays with book ISBNs as keys. So let's have this example:
    
    <?php
        $test1['24'] = 'Mary';
        $test1['17'] = 'John';
    
        $test2['67'] = 'Phil';
        $test2['33'] = 'Brandon';
    
        $result1 = array_merge($test1, $test2);
        var_dump($result1);
    
        $result2 = [...$test1, ...$test2];    // mentioned by fsb
        var_dump($result2);
    ?>
    
    You will get both:
    
    array(4) {
      [0]=>
      string(4) "Mary"
      [1]=>
      string(4) "John"
      [2]=>
      string(4) "Phil"
      [3]=>
      string(7) "Brandon"
    }
    
    Use the + operator or array_replace, this will preserve - somewhat - the keys:
    
    <?php
        $result1 = array_replace($test1, $test2);
        var_dump($result1);
    
        $result2 = $test1 + $test2;
        var_dump($result2);
    ?>
    
    You will get both:
    
    array(4) {
      [24]=>
      string(4) "Mary"
      [17]=>
      string(4) "John"
      [67]=>
      string(4) "Phil"
      [33]=>
      string(7) "Brandon"
    }
    
    The keys will keep the same, the order will keep the same, but with a little caveat: The keys will be converted to integers.
    up
    down
    3
    fsb at thefsb dot org ¶
    6 years ago
    We no longer need array_merge() as of PHP 7.4.
    
        [...$a, ...$b]
    
    does the same as
    
        array_merge($a, $b)
    
    and can be faster too.
    
    https://wiki.php.net/rfc/spread_operator_for_array#advantages_over_array_merge
    +add a note
    • Array Functions
      • array
      • array_​all
      • array_​any
      • array_​change_​key_​case
      • array_​chunk
      • array_​column
      • array_​combine
      • array_​count_​values
      • array_​diff
      • array_​diff_​assoc
      • array_​diff_​key
      • array_​diff_​uassoc
      • array_​diff_​ukey
      • array_​fill
      • array_​fill_​keys
      • array_​filter
      • array_​find
      • array_​find_​key
      • array_​first
      • array_​flip
      • array_​intersect
      • array_​intersect_​assoc
      • array_​intersect_​key
      • array_​intersect_​uassoc
      • array_​intersect_​ukey
      • array_​is_​list
      • array_​key_​exists
      • array_​key_​first
      • array_​key_​last
      • array_​keys
      • array_​last
      • array_​map
      • array_​merge
      • array_​merge_​recursive
      • array_​multisort
      • array_​pad
      • array_​pop
      • array_​product
      • array_​push
      • array_​rand
      • array_​reduce
      • array_​replace
      • array_​replace_​recursive
      • array_​reverse
      • array_​search
      • array_​shift
      • array_​slice
      • array_​splice
      • array_​sum
      • array_​udiff
      • array_​udiff_​assoc
      • array_​udiff_​uassoc
      • array_​uintersect
      • array_​uintersect_​assoc
      • array_​uintersect_​uassoc
      • array_​unique
      • array_​unshift
      • array_​values
      • array_​walk
      • array_​walk_​recursive
      • arsort
      • asort
      • compact
      • count
      • current
      • end
      • extract
      • in_​array
      • key
      • key_​exists
      • krsort
      • ksort
      • list
      • natcasesort
      • natsort
      • next
      • pos
      • prev
      • range
      • reset
      • rsort
      • shuffle
      • sizeof
      • sort
      • uasort
      • uksort
      • usort
    • Deprecated
      • each
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.