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 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 

Comments

Post a Comment

Popular posts from this blog

Difference between var, let and const keyword in JavaScript

Salesforce Record Id

Salesforce External ID