Skip to main content
2 of 2
Updated details
mk23
  • 1.4k
  • 1
  • 13
  • 27

This can be done by array.splice(). Please note array_splice or array_merge doesn't preserve keys for associative arrays. So array_slice is used and '+' operator is used for concatenating the two arrays.

More details here



$array_1 = array(
    '0' => 'zero',
    '1' => 'one',
    '2' => 'two',
    '3' => 'three',
  );
  
  $array_2 = array(
    'zero'  => '0',
    'one'   => '1',
    'two'   => '2',
    'three' => '3',
  );
  $index = 2;
  $finalArray = array_slice($array_1, 0, $index, true) +
                $array2  +
                array_slice($array_2, $index, NULL, true);
print_r($finalArray);
/*
Array
(
    [0] => zero
    [1] => one
    [10] => grapes
    [z] => mangoes
    [two] => 2
    [three] => 3
)
*/




mk23
  • 1.4k
  • 1
  • 13
  • 27