You can use the PHP array_merge() function to merge the elements or values of two or more arrays together into a single array. The merging is occurring in such a way that the values of one array are appended to the end of the previous array. Let's check out an example.
array_merge() Definition and Usage
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See example below).
<?php
$arr1 = array("Delhi","Lucknow");
$arr2 = array("Mumbai","Noida");
print_r(array_merge($arr1,$arr2));
?>
Output
Array ( [0] => Delhi[1] => Lucknow[2] => Mumbai[3] => Noida)
Example for Merge two associative arrays into one array
<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));
?>
Output
Array
(
[a] => red
[b] => yellow
[c] => blue
)
No comments:
Post a Comment