加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Array_splice充当array_slice?

发布时间:2020-12-13 18:14:16 所属栏目:PHP教程 来源:网络整理
导读:我有问题确切了解array_splice和array_slice的作用.据我所知,array_splice应该返回一个数组,取出某些元素后,array_slice应该检索一个数组的切片. php.net/array_splice手册中的以下代码显示此代码应该有效. $input = array("red","green","blue","yellow");v
我有问题确切了解array_splice和array_slice的作用.据我所知,array_splice应该返回一个数组,取出某些元素后,array_slice应该检索一个数组的切片.

php.net/array_splice手册中的以下代码显示此代码应该有效.

$input = array("red","green","blue","yellow");
var_dump(array_splice($input,2));
// $input is now array("red","green")

$input = array("red","yellow");
var_dump(array_slice($input,"green")

但是,当我在php 5.3.4和5.1.6上运行此代码时,结果是

array
  0 => string 'blue' (length=4)
  1 => string 'yellow' (length=6)
array
  0 => string 'blue' (length=4)
  1 => string 'yellow' (length=6)

我误解了手册还是这个错误?在我看来,array_splice就像array_slice一样

它似乎也不做替换

$input = array("red",2,array('foo')));

输出

array
  0 => string 'blue' (length=4)
  1 => string 'yellow' (length=6)

有人可以证实这是一个错误,如果没有解释这应该如何工作?

编辑:

Nvm我明白了.我没有在array_splice上使用var_dump,而是使用$input作为array_splice更改$input而不是返回新值.

array_slice返回值,而array_splice将值设置为$input.

MOD请关闭或删除此.

从 the docs开始:

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

Removes the elements designated by offset and length from the input array,and replaces them with the elements of the replacement array,if supplied.

Return Values

Returns the array consisting of the extracted elements.

你只是可怕,非常困惑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读