Home > Article > Backend Development > php array_splice definition and usage_PHP tutorial
I have talked about the usage of php array_splice before. Today I will mainly talk about some detailed and specific situations of this function in the application. Friends in need can refer to it.
array_splice definition and usage
Description
array array_splice ( array &$input , int $offset [, int $length [, array $replacement ]] )
array_splice() removes the elements specified by offset and length from the input array, and replaces them with elements from the replacement array if the replacement argument is provided. Returns an array containing the removed cells. Note that numeric key names in input are not preserved.
If offset is positive, removal begins at the offset specified by this value in the input array. If offset is negative, removal begins at the offset specified by this value from the end of input.
If length is omitted, all parts of the array from offset to the end are removed. If length is specified and is positive, this many cells are removed. If length is specified and is negative, all elements from offset to length counting down from the end of the array are removed. Tip: To remove all elements from offset to the end of the array when replacement is given, use count($input) as length.
If a replacement array is given, the removed cells are replaced by cells in this array. If the combination of offset and length results in no value being removed, the element in the replacement array will be inserted at the position specified by offset. Note that the key names in the replacement array are not retained. If the value being replaced is just a cell, there is no need to add array() to it, unless the cell itself is an array.
The following expression modifies $input in the same way: array_splice() Equivalent expression array_push($input, $x, $y) array_splice($input,
The code is as follows | Copy code | ||||
count($input), 0, array($x, $y))
array_shift($input) array_splice($input, 0, 1) |
代码如下 | 复制代码 |
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); $a2=array(0=>"Tiger",1=>"Lion"); print_r(array_splice($a1,0,2,$a2)); ?> |
The code is as follows | Copy code |
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); $a2=array(0=>"Tiger",1=>"Lion"); print_r(array_splice($a1,0,2,$a2)); ?> |
输出:
Array ( [0] => Dog [1] => Cat )例子 3
length 参数设置为 0:
代码如下 | 复制代码 | ||||
$a1=array(0=>"Dog",1=>"Cat");
array_splice($a1,1,0,$a2); print_r($a1);?> |
Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )
代码如下 | 复制代码 |
$input1 = array("red", "green", "blue", "yellow"); |
应用实例
代码如下 | 复制代码 |
$input1 = array("red", "green", "blue", "yellow");
$input2 = array_splice($input1, 2); |
print_r($input1);
代码如下 | 复制代码 |
$input = array("red", "green", "blue", "yellow"); array_splice($input, 2); print_r($input); |
print_r($input2);
因为这个函数的第一个参数是地址引用,返回值是被移走的部份。如果你只是想看剩下的。这样写 就可以了。
代码如下 | 复制代码 |
$input = array("red", "green", "blue", "yellow"); array_splice($input, 2); print_r($input); |
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
//从第2个之后开始选,到剩下的全部,选中的移走。
//也就是 "blue", "yellow" 被选中
array_splice($input, 1, -1);
//从第1个之后开始选,到剩下的全部倒回来一个,选中的移走。
//也就是 "green", "blue",被选中
array_splice($input, 1, count($input), "orange");
//从第1个之后开始选,到剩下的全部,选中的移走,在当前指针位置加一个新值。
//也就是 "green", "blue", "yellow" 被选中
array_splice($input, -1, 1, array("black", "maroon"));
//从最后1个之前开始选,往下选1个,选中的移走,在当前指针位置加进一个数组。
//也就是 "yellow" 被选中
//从第3个之后开始选,一个都不选,在当前指针位置插入新值。
http://www.bkjia.com/PHPjc/445319.htmlTechArticle关于php array_splice的用法以前有讲过了,今天主要是详细的讲一下这个函数在应用中的一些详细的具体的情况,有需要的朋友可以参考一下。...