在PHP中的特定值之后在数组中添加新元素?

时间:2018-04-07 06:10:18

标签: php

数组如下 -

padding-left:15px;

我想在[3] =>之后在数组中添加新值'电子邮件'

我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用 array_splice():

<?php

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

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

print_r($array);

答案 1 :(得分:0)

您可以使用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 );