What is difference between array_merge and array_combine?

Sharad Jaiswal
Sharad Jaiswal

Posted On: Feb 22, 2018

 

    2 Answers Written

  •  Sharad Jaiswal
    Answered by meenakshi

    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.

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

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

    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.

     

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

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

  •  Sharad Jaiswal
    Answered by Satyam

    Main differences between Array_merge and array_combine are

    Array_merge: Array_merge() defines a function that merges the elements of single or multiple arrays into an individual resultant array in a way that all the values of an array append to the end of the former ones. One or more array can be passed as the parameters.

    If the input arrays are having the same string keys then their resultant value for the key will overwrite the former key and if the array contains the numerical keys then the latter values will not overwrite the original value and it will be appended. In the input array values with numerical keys will be remembered easily with the incrementing keys initiating from 0 in the resultant array.

    If only one array is assigned to the Array_merge function () danger keys are integers and function returns the new array along with the integer keys initiating from 0 and then gradually increased by 1 for every consequent value. Let us learn about its syntax-

    Syntax : array_merge (array1,array2...)

    Explanation: array1: Specifies an input array. (Required) ,array2: Specifies an input array. (Optional)

    If the array has the same strings then the value for the last array for the specific key will overwrite the previous values. If the array contains the numerical keys then the value for last will not overwrite the value of previous ones and then append the last of the resultant array.

    Array_combine: Array_combine() defines a function which takes to the array of similar length and then creates a new resultant array by utilizing one of it as key and the other one for the values. This function also returns the combination array on success and FALSE when the length of them do not match.

    While using the Array_combine function () it is essential that entire values in both the arrays must be similar. Let us learn about its syntax-

    Syntax : array_combine (array1, array2); Explanation:

    array1: Specifies an input array. (Required) ,array2: Specifies an input array. (Required)

    It is required when both the arrays have an equal number of elements and if you are not, a function returns the value as FALSE For the empty arrays function issues E_WARNING and returns the value false with the PHP version.

Related Questions

Please Login or Register to leave a response.

Related Questions

PHP Array Interview Questions

What is an array in PHP?

PHP Array is a special type of variable that stores multiple values in a single variable. There are 3 types of array available in PHP they areIndexed arrays Associative arrays Multidimensional a...

PHP Array Interview Questions

How to create an empty array in PHP?

It is very easy to create an array in PHP. You can create an array in PHP using array() function or simply assigning [] in front of variable. $array1=array() or $array1=[];...

PHP Array Interview Questions

How to get number of elements in an Array?

You can use count() function to count the number of elements in a PHP Array. $array=['ram','ravi','ajay','vijay']; echo count($array); // outputs 4 ...