Open In App

PHP | Merging two or more arrays using array_merge()

Last Updated : 18 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




    <?php
      
    $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:




    <?php
      
    $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     
    )




    <?php
      
    $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
    )
    


Previous Article
Next Article

Similar Reads

What is the difference between array_merge and array + array in PHP?
In PHP arrays can be joined using array union (+) operator or by using array_merge() function. There is a subtle differences in these two methods. The array_merge() function is an inbuilt function for joining two array regardless of their type (numerical, categorical etc.)array_merge() Function: The array_merge() merges one or more arrays provided
4 min read
Difference between array_merge() and array_combine() functions in PHP
array_merge() Function: The array_merge() function 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 l
3 min read
How to use array_merge() and array_combine() in PHP ?
In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() Function: This function merges the two or more arrays such that all the arrays have keys a
3 min read
What is the differences between array_merge() and array_merge_recursive() functions in PHP ?
In this article, we will see the array_merge() and array_merge_recursive() functions, along with understanding their basic implementation, &amp; the differences between them. Both the array_merge() Function and array_merge_recursive() function can be used to combine multiple arrays into a single array. Table of Content PHP array_merge() FunctionPHP
3 min read
Find the common elements of more than two JavaScript arrays ?
Given an HTML document having multiple arrays with some elements and the task is to get the common elements from arrays with the help of JavaScript. There are two approaches that are discussed below. Approach 1: First get the arrays in 2-dimensional format then take the first array by shift() method and then filter out the elements from the first a
3 min read
How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ?
In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can al
2 min read
Javascript Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5-&gt;10-&gt;15-&gt;40 b: 2-&gt;3-&gt;20 Output: res: 40-&gt;20-&gt;15-&gt;10-&gt;5-&gt;3-&gt;2 Input: a: NULL b: 2-&gt;3-&gt;20 Output: res: 20-&gt;3-&gt;2Recommended: Please solve it on "PRACTIC
4 min read
How to add more values to array on button click using PHP ?
In this article, we will learn to add elements to an existing array with the click of a button using PHP. PHP is a server-side language, and it only responds to requests (GET, POST, PUT, PATCH, and DELETE). The button click action happens as a part of the client-side to directly call a PHP function. We need an intermediary language to perform this
3 min read
What is Declaration Merging in Typescript ?
In Typescript, the term "declaration merging" refers to the compiler combining two declarations with the same name into a single definition. Both of the initial declarations are present in this combined definition. It is possible to merge Interfaces, namespaces and enums and so on but classes cannot be merged. Interface with Interface Merging: In t
3 min read
Git - Difference Between Merging and Rebasing
Git offers several methods to integrate changes from one branch into another, two of the most commonly used are merging and rebasing. While both serve similar purposes, they have distinct functionalities and implications. In this article, we will discuss both of them, their benefits and differences, and their typical workflow. Git mergeThe easiest
2 min read