PHP array_merge()

array_merge() function is used to merge one or more than one arrays into a single resultant array.

Main Features of array_merge() Function

  1. It can merge more than two functions.
  2. Arrays may have a different number of elements.
  3. Elements having the same key are overwritten by the last elements.
  4. We can pass one, two or many arrays to merge.
  5. In case of a single array is passed to merge and it is an Indexed array, The output array is having index starts from zero(0).
<?php
$marval_heros = ['Iron Man','Captain America','Hulk','Spidar Man'];
$dc_heros = ['Bat Man','Super Man'];
$indian_heros = ['Kriss','Chitthi','Shaktimaan'];

//merge all super hero array
$all_super_heros = array_merge($marval_heros,$dc_heros,$indian_heros);
print_r($all_super_heros);

$all_marvel_heros = array_merge($marval_heros);
print_r($all_marvel_heros);
?>

The output of Above code samples:

(1) When $marvel_heros are merged only
Array
(
    [0] => Iron Man
    [1] => Captain America
    [2] => Hulk
    [3] => Spidar Man
)
(2) When all are merged
Array
(
    [0] => Iron Man
    [1] => Captain America
    [2] => Hulk
    [3] => Spidar Man
    [4] => Bat Man
    [5] => Super Man
    [6] => Kriss
    [7] => Chitthi
    [8] => Shaktimaan
)
Your Result displays here
Memory: Memory Usage, CPU Time: CPU Usage