Home » General » Difference between array_merge and array_combine in PHP

Difference between array_merge and array_combine in PHP

posted in: General 0

Array Merge

Function array_merge() merges the elements of one or more than one array such that the value of one array appended at the end of the first array.

If arrays have the same key then later value overrides the previous value for that key.

For Ex:

<?php $array1 = array(“course1” => “java”,”course2″ => “sql”);
$array2 = array(“course1” => “php”,”course3″ => “html”);
$result= array_merge($array1, $array2);
print_r($result); ?>

Result
Array
(
[course1] => php
[course2] => sql
[course3] => html
)

 

Array Combine

Characteristic array_combine() is used to creates a brand new array with the aid of the use of the important thing of one array as keys and the usage of the one issue to preserve in thoughts whilst the use of array_combine() that variety of values in both arrays have to be identical.

<?php $array1 = array(“course1″,”course2″,”course3”);
$array2 = array(“php”,”html”,”css”);
$result = array_combine($array1, $array2);
print_r($result); ?>

Result
Array
(
[course1] => php
[course2] => html
[course3] => css
)