我需要删除数组中给定变量之前的值并存储在同一个键中,这要事先感谢。
$filter = 8;
Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[13] => PHM
[14] => PHN
[15] => N
[17] => M9
[18] =>
)
我的输出应该是,
Array(
[8] =>
[9] =>
[10] =>
[11] =>
[13] => PHM
[14] => PHN
[15] => N
[17] => M9
[18] =>
)
答案 0 :(得分:6)
您可以使用array_slice()
执行此操作。既然你没有一个基于0的枚举数组,你必须确保设置
preserve_keys参数为true:
project.json
答案 1 :(得分:2)
$arr = array("","","","","","","","","","","","","","PHM","PHN","N","","M9","");
$filter = 8;
For($i=0;$i<$filter;$i++){
If(isset($arr[$i])) unset($arr[$i]);
}
Var_dump($arr);
对于这个例子,我创建了一个包含所有索引的数组,但是即使因为isset()而缺少0和12也会有效。
编辑保留关键8.