array_combine and array_merge are the array functions in PHP that are used for different functionalities both are different from each other both have different functions.
Merging the elements basically means to merge one or more arrays together so that the values of one are added to the end of the previous array.array_merge function in PHP is used to merges the elements of one or more than one array in such a manner that the value of one array adjoin at the end of first array. If the arrays have identical strings key then the last array value overrides the previous value for that key .
Example of merge_array
<?php
$array1 = array(color => blue, 4, 6);
$array2 = array(x, y, color => black, shape => square, 6);
$result = array_merge($array1, $array2);
print_r($result);
?>
Output for the above code is:
Array
(
[color] => black
[0] => 4
[1] => 6
[2] => x
[3] => y
[shape] => square
[4] => 6
)
array_combine function in PHP creates an array in array_combine one array is used for keys and another is used for its values. It gives the output with the combine array of array 1 and array 2 .
<?php
$array1 = array(blue,yellow,pink);
$array2 = array(grapes, peach, papaya);
$result = array_combine($array1, $array2);
print_r($result);
?>
Output for the above code is:
Array
(
[blue] => grapes
[yellow] => peach
[pink] => papaya
)
0 Comment(s)