- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我无法准确理解 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", "green", "blue", "yellow");
var_dump(array_slice($input, 2));
// $input is now array("red", "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", "green", "blue", "yellow");
var_dump(array_splice($input, 2, 2, array('foo')));
输出
array
0 => string 'blue' (length=4)
1 => string 'yellow' (length=6)
有人可以确认这是一个错误吗?如果不能,请解释一下这应该如何工作?
编辑:
Nvm 我想通了。我不应该在 array_splice 上使用 var_dump,而是在 array_splice 更改 $input 而不是返回新值时使用 $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.
你只是非常非常困惑。
关于php - Array_splice 充当 array_slice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5901673/
我有一个 PHP 数组,里面有 50 个项目。然后我使用 array_slice 提取 12 个项目: $extractItems = array_slice($rows,0,12); 然后我使用 $
由于我不会在这里讨论的原因,我不得不解析一个大的文件目录(我们说的是 100.000 < x < 1.000.000+)并将文件列表作为数组返回。 我已经在缓存文件列表了,问题是array_slice
我是 php 的新手。我想在一个大数组上做多个数组切片。现在这是我的代码的一部分: $row1 = array_slice($filtered, 0, 22); $row2 = array_slice
更新 我是 PHP 开发新手:我在 PHP 网站上查找了一个函数 - array_slice。我阅读并查看了示例,但我不明白。谁能用清楚的话给我解释一下? 我认为它的工作原理如下? $example
我有一个以索引作为时间戳的数组。我试过 array_slice获取某个时间范围内的所有值,但它似乎不起作用。 $data = array_slice(["1549440811" => 1, "1549
我只想做一件事,我有一个包含产品的数组,可以这样说(以简单的方式): $products = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j
我正在尝试删除对象中的前两项。例如,如果我想从数组中删除前两项,我会使用 array_slice($arrayName, 2)。 我已经在我的对象上试过了(嘿,为什么不呢?我知道它在技术上不是一个数组
我无法准确理解 array_splice 和 array_slice 的作用。据我所知,array_splice 应该在取出某些元素后返回一个数组,而 array_slice 应该检索数组的一部分。
一些背景 我正在参加常见的“MaxProfit”编程挑战。它基本上是这样的: Given a zero-indexed array A consisting of N integers contain
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 去年关闭。 Im
我有一个搜索查询,它从搜索表中查找匹配值并返回与关键字匹配的所有可能条目。我没有编写搜索,它是已经存在的东西,我们正在将其移植到另一个框架。无论如何,这不是重点。 我想要的是使用 LIMIT 执行 M
如何在 PHP 中使用 array_splice/array_slice 删除特定项目? 例如:数组('a','b','c');如何删除'b'?所以数组仍然是:数组('a','c'); 谢谢 最佳答案
我有一个关于 ArrayObject 的问题。我想在 ArrayObject 类中使用 array_slice 但我不能。有没有办法做到这一点,而不需要为实现 ArrayObject 的类编写“切片”
我想在我的 PHP 脚本中使用 array_slice 和 scandir。 正常使用: ! [1] => . [2] => .. ) 一般来说,这对于所有以 . 之前的字符开头的文
我是一名优秀的程序员,十分优秀!