Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between array_merge() and array_combine()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 10.3k
    Comment on it

    PHP have a vast collection of array functions which makes the use and manipulation of arrays much easier. This article defines the difference between two important array functions array_merge and array_combine which sounds similar but used for different operations.

     

    array_merge() : array_merge() function merges the elements of one or more arrays into a single resultant array in such a way, So that the values of one array append to the end of previous ones. We can pass one or more arrays as parameters.

     

    Syntax : array_merge(array1,array2...)

    array1 : Specifies an input array. (Required)

    array2 : Specifies an input array. (Optional)

     

    Note :

    1. If the arrays have same string keys, then the value of last array for that key will overwrite the value of previous ones.

    2. If the arrays contain numeric keys, then the values of last array will not overwrite the values of previous arrays and will be append on last of the resultant array.

    3. If there is only one array with numeric keys, Then the key of resultant array will be re-indexed and start from 0.

     

    Example 1 :

    <?php
    
    $array1 = array(1, 'a' => "red", 8, 'b' => "fruits");
    $array2 = array('a' => "green", 2, 'xyz', 4, 'b' => "test");
    $result = array_merge($array1, $array2);
    print_r($result);
    
    ?>

    Output :

    Array ( [0] => 1 [a] => green [1] => 8 [b] => test [2] => 2 [3] => xyz [4] => 4 )

     

    Example 2 : The keys of final array re-indexed if array1 and array2 have numeric indexes.

    <?php
    
    $array1 = array(0 => 'zero', 1 => 'one', 2 => 'two');
    $array2 = array(1 => 'One', 3 => 'Three', 4 => 'Four');
    $final = array_merge($array1, $array2);
    print_r($final);
    
    ?>

    Output :

    Array ( [0] => zero [1] => one [2] => two [3] => One [4] => Three [5] => Four )

     

     

    array_combine(): array_combine() function takes two arrays of same length and creates a new resultant array, using one array as keys and other array for values. The function returns the combined array on success and FALSE if the length of both array does not match.

     

    Syntax : array_combine(array1,array2);

    array1 : Specifies an input array. (Required)

    array2 : Specifies an input array. (Required)

     

    Note :

    1. It is required that both arrays should have equal number of elements. If number of elements are not same on both arrays, the function returns FALSE.

    2. The function issues E_WARNING and returns FALSE for empty arrays with PHP versions before 5.4.

     

    Example :

    <?php
    
    $roll = array(1, 2, 3, 4);
    $name = array("Amit", "Deepak", "Rahul", "Shyam");
    
    $students = array_combine($roll,$name);
    print_r($students);
    
    ?>

    Output :

    Array ( [1] => Amit [2] => Deepak [3] => Rahul [4] => Shyam )

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: