Array_splice作为array_slice? [英] Array_splice acting as array_slice?

查看:75
本文介绍了Array_splice作为array_slice?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有理解到底是什么array_splice和array_slice做的问题。从我可以告诉array_splice应该返回一个阵列中后取出特定元素和array_slice应检索数组的一个切片。

I'm having issues understanding exactly what array_splice and array_slice do. From what I can tell array_splice should return an array AFTER taking out certain elements and array_slice should retrieve a slice of the array.

php.net/array_splice 手册下code表明,该code应该工作。

The following code from the php.net/array_splice manual shows that this code should work.

$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运行此code中的结果是

However when I run this code on php 5.3.4 and 5.1.6 the results are

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

Am I misunderstanding the manual or is this a bug? It looks to me like array_splice is acting just like array_slice

此外,它似乎并没有做替换

Also it does not seem to do replacements

$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)

有人可以证实这是一个错误,如果不能解释这个应该怎么工作的?

Can someone confirm this is a bug and if not explain how this SHOULD work?

编辑:

NVM我想通了。而不是使用上array_splice一个的var_dump我应该使用$输入作为array_splice改变$输入,而不是返回新值。

Nvm I figured it out. Instead of using a var_dump on the array_splice I should be using $input as array_splice changes $input instead of returning the new values.

array_slice而array_splice设定值$输入返回的值。

array_slice returns the values while array_splice sets the values to $input.

的MOD请关闭或删除。

MODs please close or delete this.

推荐答案

文档

说明结果
  阵列array_splice(阵列&安培; $输入,整数$抵消[摘要$长度= 0,混合$替换]])

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.

...

返回值

返回由所提取的元件的阵列

Returns the array consisting of the extracted elements.

您只是可怕,可怕的混乱。

You are just horribly, horribly confused.

这篇关于Array_splice作为array_slice?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆