Array_merge
Array_merge merges the elements of two or more array in one array such that the value of second array is appended to the value of first array. If two or more array elements have same key, then the later value will overrides the previous value for that key.
If you assign only one array to the array_merge() function having integer keys then the function will return an array with integer keys starting from 0 and increases by 1 for each value. You can assign any number of arrays to array_merge() function as you need.
Example: Two arrays having integer keys
<?php
$ar1=array("lion","tiger");
$ar2=array("leopard","bear");
print_r(array_merge($ar1,$ar2));
?>
output: Array ( [0] => lion [1] => tiger [2] => leopard [3] => bear )
Example: Two associative arrays
<?php
$ar1=array("a"=>"lion","b"=>"tiger");
$ar2=array("c"=>"leopard","b"=>"bear");
print_r(array_merge($ar1,$ar2));
?>
output: Array ( [a] => lion [b] => bear [c] => leopard )
Example: Only one array parameter having integer keys
<?php
$ar=array(3=>"lion",4=>"tiger");
print_r(array_merge($ar));
?>
output: Array ( [0] => lion [1] => tiger )
array_combine()
Array_combine returns a new array by making the keys from the values of first array and value from the values of second array
i.e it returns the combine array of array1 and array2 .
array_combine(ar1,ar2);
array_combine(keys,values);
Example:
<?php
$ar1=array("a","b","c");
$ar2=array("5","7","9");
$ar3=array_combine($ar1,$ar2);
print_r($ar3);
?>
output: Array ( [a] => 5 [b] => 7 [c] => 9 )
0 Comment(s)