Posts Tagged ‘array_merge’
Merging two arrays is required very commonly but most of times you require indexes to remain same. There is a function called array_merge(array1, array2,…) which can merge any number of arrays but it will cause two problems mainly. 1: Order of entries will be changed and 2: indexes will be refreshed from 0 onwards.
Here the magic of php comes you just need a “+” sign to merge arrays.
$array1 = array(0 => 'val', 1 => 'another val') $array2 = array('my_val' => 'my val', 13 => 'val 13') $array = $array1 + $array1; /* $array will become array( 0 => 'val',1 => 'another val', 'my_val' => 'my val', 13 => 'val 13') */