Home  >  Article  >  Backend Development  >  php array_splice definition and usage_PHP tutorial

php array_splice definition and usage_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:021081browse

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))
 代码如下 复制代码
count($input), 0, array($x, $y)) 
array_pop($input)  array_splice($input, -1) 
array_shift($input)  array_splice($input, 0, 1) 
array_unshift($input, $x, $y)  array_splice($input, 0, 0, array($x, $y)) 
$input[$x] = $y // 对于键名和偏移量等值的数组  array_splice($input, $x, 1, $y) 
array_pop($input) array_splice($input, -1)

array_shift($input) array_splice($input, 0, 1)
array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y))

$input[$x] = $y // For arrays with key names and offsets array_splice($input, $x, 1, $y)

Returns an array containing the removed cells.

 代码如下 复制代码
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
print_r(array_splice($a1,0,2,$a2));
?>
print_r($a1); ?> Output: Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird ) Example 2 Same as example 1, but prints the returned array:
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");
 代码如下 复制代码
$a1=array(0=>"Dog",1=>"Cat");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,1,0,$a2);
print_r($a1);
?>
$a2=array(0=>"Tiger",1=>"Lion");

array_splice($a1,1,0,$a2);

print_r($a1);

?>

输出:


Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )

 代码如下 复制代码

$input1 = array("red", "green", "blue", "yellow");
$input2 = array_splice($input1, 2);

应用实例

 代码如下 复制代码
$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" 被选中

// $input is now array("red", "green")


array_splice($input, 1, -1);
//从第1个之后开始选,到剩下的全部倒回来一个,选中的移走。
//也就是 "green", "blue",被选中

// $input is now array("red", "yellow")


array_splice($input, 1, count($input), "orange");
//从第1个之后开始选,到剩下的全部,选中的移走,在当前指针位置加一个新值。
//也就是 "green", "blue", "yellow" 被选中

// $input is now array("red", "orange")


array_splice($input, -1, 1, array("black", "maroon"));
//从最后1个之前开始选,往下选1个,选中的移走,在当前指针位置加进一个数组。
//也就是 "yellow" 被选中

// $input is now array("red", "green","blue", "black", "maroon")
array_splice($input, 3, 0, "purple");

//从第3个之后开始选,一个都不选,在当前指针位置插入新值。

//位置就在 "red", "green", "blue" 和 "yellow" 之间 http://www.bkjia.com/PHPjc/445319.htmlwww.bkjia.comtrue
http://www.bkjia.com/PHPjc/445319.htmlTechArticle
关于php array_splice的用法以前有讲过了,今天主要是详细的讲一下这个函数在应用中的一些详细的具体的情况,有需要的朋友可以参考一下。...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn