Merging Two Arrays in php

The array_merge() function intelligently merges two or more arrays:

$merged = array_merge(array1, array2 [, array … ])

If a numeric key from an earlier array is repeated, the value from the later array is assigned a new numeric key:

Example of array_merge

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
$first = array("hello", "world"); // 0 => "hello", 1 => "world"
$second = array("exit", "here"); // 0 => "exit", 1 => "here"
$merged = array_merge($first, $second);
// $merged = array("hello", "world", "exit", "here")

Leave a Comment