Sunday 15 January 2017

Array_merge and Array_combine in php

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.

<?php
$array1    = array("subject1","subject2","subject3");
$array2    = array("php","html","css");
$new_array = array_combine($array1, $array2);
print_r($new_array);
?>

Result
Array Combine Array ( [subject1] => php [subject2] => html [subject3] => css )

<br>

Array Merge
array_merge merges one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings  key  then the later value overrides the previous value for that key .

<?php
$array1 = array("one" => "java","two" => "sql");
$array2 = array("one" => "php","three" => "html","four"=>"Me");
$result = array_merge($array1, $array2);
print_r($result);?>

Result
Array Merge Array ( [one] => php [two] => sql [three] => html [four] => Me )

No comments:

Post a Comment