laitimes

array_slice and array_splice () are different

author:The cold flute is over the frost day

array_slice — Take a segment from the array, the original array has not changed

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] )

array_slice() Return value: The key of the array will be reset by default (false) If there are character keys in the array, the returned key name will be retained (including the name of the array key for which the number is reindexed)

true retains the key name;

$lamp=array("Linux", "Apache", "MySQL", "PHP"); //声明一个索引数组$lamp包含4个元素
//使用array_slice()从第二个开始取(0是第一个,1为第二个),取两个元素从数组$lamp中返回
print_r(array_slice($lamp,1,2)); //输出:Array ( [0] => Apache [1] => MySQL )
//第二个参数使用负数参数为-2,从后面第二个开始取,返回一个元素
print_r(array_slice($lamp,-2,1)); //输出: Array ( [0] => MySQL )
//最后一个参数设置为 true,保留原有的键值返回
print_r(array_slice($lamp,1,2,true)); //输出:Array ( [1] => Apache [2] => MySQL )
$lamp=array("a"=>"Linux","b"=>"Apache","c"=>"MySQL","d"=>"PHP"); //声明一个关联数组
//如果数组有字符串键,默认所返回的数组将保留键名
print_r(array_slice($lamp,1,2)); //输出:Array ( [b] => Apache [c] => MySQL )

0 1 2 3 4
-5 -4 -3 -2 -1           

array_splice — remove part of the array and replace it with another value, the original array changes (array &$input reference value)

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

array_splice() 把 input 数组中由 offset 和 length 指定的单元去掉,如果提供了 replacement 参数,

is replaced by a cell in the replacement array. Returns an array containing the removed cells. Note: The numeric key names in input are not retained.

$replacement如果传入的是单个值,则不能添加键名(索引和关联),否则出现Parse error: syntax error, unexpected T_DOUBLE_ARROW

If you are passing in an array, you can add a key name

If the third parameter $length is positive, it means length, and if the $length is negative, all elements in between the offset and the reciprocal length at the end of the array are removed.

The fourth parameter can be a string or an array in place of the removed value;

$input = array("Linux", "Apache", "MySQL", "PHP");
array_splice($input, 2); //原数组中的第二个元素后到数组结尾都被删除
print_r($input); //输出:Array ( [0] => Linux [1] => Apache )
$input = array("Linux", "Apache", "MySQL", "PHP");
array_splice($input, 1, -1); //从第二个开始移除直到数组末尾倒数1个为止中间所有的元素
print_r($input); //输出:Array ( [0] => Linux [1] => PHP )
$input = array("Linux", "Apache", "MySQL", "PHP");
array_splice($input, 1, count($input), "web"); //从第二个元素到数组结尾都被第4个参数替代
print_r($input); //输出:Array ( [0] => Linux [1] => web )
$input = array("Linux", "Apache", "MySQL", "PHP");
array_splice($input, -1, 1, array("web", "www")); //最后一个元素被第4个参数数组替代
print_r($input); //输出:Array ( [0] => Linux [1] => Apache [2] => MySQL [3] => web [4] => www )           

array_splice() equivalence expression

array_push($input, $x, $y) array_splice($input, 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)

Tip: Both array_slice() and array_splice() can be used for pagination when all data is available

How do I append a value to the front or back of an element in an array?

<?php
$arr = array(101, 102, 103, 104, 105, 106, 107);
$pos = 0;
$id1 = 103;
$id2 = 105;
/*
//方法一
echo reset($arr);
$newarr = array();
foreach ($arr as $key=>$value){
if($value == $id2){
unset($arr[$key]);
}else{
$newarr[] = $value;
}
if($value == $id1){
$newarr[] = $id2;
}

}
echo "新的数组是:";
var_export($newarr);
*/
//方法二
foreach ($arr as $key=>$value){
if($value == $id1){
$pos = $key;
}
if($value == $id2){
unset($arr[$key]);
}
}
echo "当前的位置:".$pos;
$newarr = array_splice($arr, $pos+1, 0, $id2); //当前的数组元素的后面
echo "新的数组是:";
var_export($arr);
?>