Home > Backend Development > PHP Tutorial > php array_splice函数使用方法的解决思路

php array_splice函数使用方法的解决思路

PHP中文网
Release: 2023-02-28 13:34:01
Original
1325 people have browsed it

简单点说,array_splice() 函数的功能,是把给定数组中从某位置开始的数组元素去掉,返回一个包含有被移除单元的数组。因此,你的疏忽之处在于:

foreach
( array_splice($array,2) as $
key
 => $value )
Copy after login

这句代码中的 array_splice($array,2) 的结果是那些 被你移除数组的元素,而不是你意想中 你想保留的数据。

你需要仔细看下 array_splice的函数原型声明:

array array_splice( array &$input   , int$off
set
 。。。。。)
Copy after login

注意:&$input 这个参数是用地址传递的方式,而不是通常的值传递,所以,调用 array_splice 函数之后,会直接将$input数组的数据进行修改。

综合上面,修改代码如下:

<?php
$array = array(1,2,3,4,5);
$array[7] = 7;
$array[] = 8;
$arr = array_splice($array,3);
foreach($array as $key => $value)
{
  echo "$key:$value<br />";
}
?>
Copy after login

 以上就是php array_splice函数使用方法解决思路的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
Statement of this Website
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
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template