Explain the difference between array_merge() and array_combine()?

1. array_combine(): It is used to creates a new array by using the key of one array as keys and using the value of another array as values.

Example :

$array1 = array('key1', 'key2', 'key3');
$array2 = array(('umesh', 'sonu', 'deepak');
$new_array = array_combine($array1, $array2);

OUTPUT : Array([key1]  => umesh[key2]    => sonu[key2]    =>deepak)

2. array_merge(): It merges one or more than one array such that the value of one array appended at the end of the first array. If the arrays have the same strings key, then the next value overrides the previous value for that key.

Example :

$array1 = array("one" => "java","two" => "sql"); 
$array2 = array("one" => "php","three" => "html","four"=>"Me"); 
$result = array_merge($array1, $array2); 
Array ( [one] => php [two] => sql [three] => html [four] => Me )

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question