Array merge and array combine difference 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 list of arrays separated by commas as a parameter that is needed to be merged and returns a new array with merged values of arrays passed in parameter.

Syntax:

array array_merge( $array1, $array2, ...., $array n)

where, $array1, $array2, . . .  are the input arrays that need to be merged.

Example: PHP program to merge two arrays.

PHP

$array1 = array("subject1" => "Python","subject2" => "sql");

$array2 = array("subject3" => "c/c++","subject4" => "java");

$final = array_merge($array1, $array2);

print_r($final);

?>


 

Output

Array
(
    [subject1] => Python
    [subject2] => sql
    [subject3] => c/c++
    [subject4] => java
)

Example 2: PHP program to merge multiple arrays.

PHP

$array1 = array("subject1" => "Python", "subject2" => "sql");

$array2 = array("subject3" => "c/c++", "subject4" => "java");

$array3 = array("subject5" => "CN", "subject6" => "OS");

$array4 = array("subject7" => "data mining", "subject8" => "C#");

$final = array_merge($array1, $array2, $array3, $array4);

print_r($final);

?>

Output

Array
(
    [subject1] => Python
    [subject2] => sql
    [subject3] => c/c++
    [subject4] => java
    [subject5] => CN
    [subject6] => OS
    [subject7] => data mining
    [subject8] => C#
)

array_combine() Function: The array_combine() function is used to combine two arrays and create a new array by using one array for keys and another array for values i.e. all elements of one array will be the keys of new array and all elements of the second array will be the values of this new array.

Syntax:

array_combine(array1, array2)

Where, array1 is the first array with keys and array2 is the second array with the values.

Example: PHP program to combine arrays.

PHP

$array1 = array("subject1" ,"subject2");

$array2 = array( "c/c++", "java");

$final = array_combine($array1, $array2);

print_r($final);

?>

Output

Array
(
    [subject1] => c/c++
    [subject2] => java
)

Example 2:

PHP

$array1 = array("subject1", "subject2", "subject3", "subject4");

$array2 = array( "c/c++", "java", "Python", "HTML");

$final = array_combine($array1, $array2);

print_r($final);

?>

Output

Array
(
    [subject1] => c/c++
    [subject2] => java
    [subject3] => Python
    [subject4] => HTML
)

Difference Between array_merge() and array_combine() Function:

array_merge() Function

array_combine() Function

This function merges the two or more arrays. This array combine only two arrays.
This function merges  the arrays such that all the arrays have keys and values. This function combine the one array containing keys and another array containing values.
The arrays are appended at the end of the first array. The arrays are combined.

What is difference between array Combine and array merge in PHP?

This function is used to merge the elements or values of two or more arrays together into a single array. ... PHP..

What is the difference between array_merge () and Array_merge_recursive () in PHP?

Definition and Usage The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

Which function combine or merge two or more arrays?

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

What is merge in array?

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.