Add an array in the middle on another associative array in php

Add an array in the middle on another associative array in php


$array_1 = array(
    '0' => 'zero',
    '1' => 'one',
    '5' => 'two',
    '6' => 'six',
  );
  
  $array_2 = array(
    '2'  => 'two',
    '3'   => 'three'
  );


  array_splice($array_1, 2, 0, $array_2);
  //Added array_2 at index 2 of array_1

  print_r($array_1);

  /* Result will be:
  Array
(
    [0] => zero
    [1] => one
    [2] => two
    [3] => three
    [4] => two
    [5] => six
)
*/

Leave a Comment

Your email address will not be published. Required fields are marked *