Difference Between array_merge() and array_combine


array_merge():-array_merge() function 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 value for that key of first array will overwrite by the value of next array. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be added at the last of the arrray.

 

Example:

 

$array1= array("name" =>"vikram", 2, 5);
$array2 = array("a", "b", "name" =>; "mohan", "designation" => "developer", 5);
$arrayMergeResult = array_merge($array1, $array2);
print_r(
$arrayMergeResult);
?>
Result of above query will:
array(
[name] => mohan     [0] => 2     [1] => 5     [2] => a     [3] => b     [destination] => developer     [4] => 5 
)

array_combine() — Creates an array by using one array as its keys and another for its values.

Example:

 

$array1 = array('name', 'designation', 'city');
$array2 = array('vikram', 'developer', 'jaipur');
$arrayCombineResult = array_combine($array1, $array2);

print_r($arrayCombineResult);
?>

The result of above query will be:-
Array (     [name]  => vikram     [designation]  => developer     [city] => jaipur )
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s