Difference Between array_merge() and array_combine
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 array.
Example:
$array1 = array("name" =>"vikram", 2, 5);
$array2 = array("a", "b", "name" =>; "mohan", "designation" => "TL", 5);
$arrayMergeResult = array_merge($array1, $array2);
print_r($arrayMergeResult);
?>
Result of above query will be:
array(
[name] => mohan,
[0] => 2,
[1] => 5,
[2] => a,
[3] => b,
[designation] => TL,
[4] => 5
)
array_merge():- array_combine() function creates a new array by using one array elements as its key and another array's elements as its value.
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
)
Very good blog...
ReplyDelete