Header menu

_________________________________________________________________________________

Thursday, 19 December 2013

Explain difference between array_merge( ) and array_combine( ) in php

ARRAY_MERGE()


array_merge() id used to merge the elements of one or more than one array in such a way  that the value of one array appends at the end of first array. If the any of the arrays have same key ,then the later value overrides the previous value for that key. The output of the function is one resultant array and this function takes two arrays as input.

ARRAY_COMBINE()


array_combine() is used to creates a new array by using the key of one array as keys and using the value of other array as values.One thing to keep in mind while using array_combine() that number of values in both arrays must be same.

Example :

<h2>ARRAY_MERGE()</h2>
<?php
$array1 = array("one" => "java","two" => "sql");


$array2 = array("one" => "php","three" => "html","four"=>"Me");
$result = array_merge($array1, $array2);
echo "<pre>"; print_r($result);

?>
<h2>ARRAY_COMBINE()</h2>
<?php
$array1    = array("one","two","three");
$array2    = array("php","html","jquery");
$result = array_combine($array1, $array2);
echo "<pre>"; print_r($result);

?>

OUTPUT:
Explain difference between array_merge( ) and array_combine( ) in php