Skip to main content
Updated details
Source Link
mk23
  • 1.4k
  • 1
  • 13
  • 27

This can be done by array.splice() More. 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',
  );

 
  array_splice($array_1, 3,$index 0,= array('sample_key'2;
 => 'sample_value'));

$finalArray = array_splicearray_slice($array_2, 3$array_1, 0,array('sample_key' =>$index, 'sample_value'));

print_r($array_1true); +
/* Result will be:
Array
(
    [0] => zero
    [1] => one $array2  +
    [2] => two
    [3] => sample_value
    [4]array_slice($array_2, =>$index, three
NULL, true)
*/;
print_r($array_2$finalArray);
 
/* Result will be :

Array
(
    [zero][0] => 0zero
    [one][1] => 1one
    [two][10] => 2grapes
    [0][z] => sample_valuemangoes
    [two] => 2
    [three] => 3
)
*/ 




This can be done by array.splice() 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',
  );

 
  array_splice($array_1, 3, 0, array('sample_key' => 'sample_value'));

  array_splice($array_2, 3, 0,array('sample_key' => 'sample_value'));

print_r($array_1);
/* Result will be:
Array
(
    [0] => zero
    [1] => one
    [2] => two
    [3] => sample_value
    [4] => three
)
*/
print_r($array_2);
 
/* Result will be :

Array
(
    [zero] => 0
    [one] => 1
    [two] => 2
    [0] => sample_value
    [three] => 3
)
*/


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
)
*/ 




Source Link
mk23
  • 1.4k
  • 1
  • 13
  • 27

This can be done by array.splice() 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',
  );


  array_splice($array_1, 3, 0, array('sample_key' => 'sample_value'));

  array_splice($array_2, 3, 0,array('sample_key' => 'sample_value'));

print_r($array_1);
/* Result will be:
Array
(
    [0] => zero
    [1] => one
    [2] => two
    [3] => sample_value
    [4] => three
)
*/
print_r($array_2);

/* Result will be :

Array
(
    [zero] => 0
    [one] => 1
    [two] => 2
    [0] => sample_value
    [three] => 3
)
*/