2.array_splice()
携带三个参数,同上,作用是删除从offset开始长度为length的子数组。
例子:
<?php $input = array("red", "green", "blue", "yellow"); array_splice($input, 2); // $input is now array("red", "green") $input = array("red", "green", "blue", "yellow"); array_splice($input, 1, -1); // $input is now array("red", "yellow") $input = array("red", "green", "blue", "yellow"); array_splice($input, 1, count($input), "orange"); // $input is now array("red", "orange") $input = array("red", "green", "blue", "yellow"); array_splice($input, -1, 1, array("black", "maroon")); // $input is now array("red", "green", // "blue", "black", "maroon") $input = array("red", "green", "blue", "yellow"); array_splice($input, 3, 0, "purple"); // $input is now array("red", "green", // "blue", "purple", "yellow"); ?> |
延伸阅读:
删除数组元素实用的PHP数组函数
php数组技巧二则
php遍历数组的三种方法(foreach、while、for)性能比较
PHP数组实例总结
JavaScript数组系列函数应用