difference between array_merge() and array_combine() functions in php?
In this post, we will show you difference between array_merge() and array_combine() functions in PHP
1. Code:
<?php $array1 = array("subject1" ,"subject2"); $array2 = array( "c/c++", "java"); echo"Combine two arrays:"; $final = array_combine($array1, $array2); print_r($final); echo"<br>"."Display merged arrays:"; $final = array_merge($array1, $array2); print_r($final); ?>
2. Example:
Input:
$array1 = array("subject1" ,"subject2"); $array2 = array( "c/c++", "java");
Output:
Combine two arrays: Array ( [subject1] => c/c++ [subject2] => java ) Display merged arrays: Array ( [0] => subject1 [1] => subject2 [2] => c/c++ [3] => java )
3. Difference:
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. |