3

Array as per below-

Array ( [1] => staff [2] => Name [3] => Email [4] => surname [5] => registrationno )

I want to add the new value in array after [3] = > 'Email'

What should I do?

Priyanka Khade
  • 450
  • 4
  • 18
  • 2
    Possible duplicate of [How to insert element into arrays at specific position?](https://stackoverflow.com/questions/3353745/how-to-insert-element-into-arrays-at-specific-position) – Mehravish Temkar Apr 07 '18 at 06:19
  • Use [`array_splice()`](http://php.net/manual/en/function.array-splice.php). – axiac Apr 07 '18 at 06:39
  • Please search for previous questions before posting new one's. That's what stackoverflow recommends. – Krishnadas PC Apr 25 '18 at 17:38

2 Answers2

1

use array_splice():

<?php

$array = array( 
    '1' => 'staff',
    '2' => 'Name',
    '3' => 'Email',
    '4'=> 'surname',
    '5' => 'registrationno',
);

array_splice($array, 3, 0, 'myvalue');

print_r($array);
TarangP
  • 2,711
  • 5
  • 20
  • 41
0

You can use array_splice();

$input = array ( [1] => staff [2] => Name [3] => Email [4] => surname [5] => registrationno ); array_splice($input, 3, 0, "myvalue"); //$input is now array ( [1] => staff [2] => Name [3] => Email [4] => myvalue [5] => surname [6] => registrationno );

dedy
  • 36
  • 5