19.PHP数组之 array_splice() 函数

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )

input 数组中由 offsetlength 指定的单元去掉,如果提供了 replacement 参数,则用其中的单元取代。

注意 input 中的数字键名不被保留。 

notice

1. 如果省略 length,则移除数组中从 offset 到结尾的所有部分。如果指定了 length 并且为正值,则移除这么多单元。如果指定了 length 并且为负值,则移除从 offset 到数组末尾倒数 length 为止中间所有的单元。小窍门:当给出了 replacement 时要移除从 offset 到数组末尾所有单元时,用 count($input) 作为 length。 

2. 如果 offset 为正,则从 input 数组中该值指定的偏移量开始移除。如果 offset 为负,则从 input 末尾倒数该值指定的偏移量开始移除

$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")

posted @ 2016-05-02 20:41  MatthewBlog  阅读(103)  评论(0)    收藏  举报

页脚