Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-使用偏移量创建ArrayInterator_Php_Arrays_Iterator_Offset - Fatal编程技术网

PHP-使用偏移量创建ArrayInterator

PHP-使用偏移量创建ArrayInterator,php,arrays,iterator,offset,Php,Arrays,Iterator,Offset,也许是个愚蠢的问题,但我有一个数组,我想创建一个带有偏移量的数组迭代器 例如,我有以下数组: $fruits = [ 1 => 'banana', 2 => 'apple', 3 => 'orange', ... 20 => 'raspberry' ] 我的偏移量是3 我想用$FROUTS数组和$offset创建一个ArrayInterator。 我该怎么做?我不明白ArrayIterator医生的意思。 有OffsetSet()方法,但我不明白它是

也许是个愚蠢的问题,但我有一个数组,我想创建一个带有偏移量的数组迭代器

例如,我有以下数组:

$fruits = [
  1 => 'banana',
  2 => 'apple',
  3 => 'orange',
  ...
  20 => 'raspberry'
]
我的偏移量是3

我想用$FROUTS数组和$offset创建一个ArrayInterator。 我该怎么做?我不明白ArrayIterator医生的意思。
有OffsetSet()方法,但我不明白它是如何工作的,要么在循环时跳过,要么使用
array\u splice
从偏移开始创建新数组

$obj = new ArrayObject( array_slice($fruits, 3) );
$it = $obj->getIterator();

你的意思是跳过数组中的前x个元素,是吗?或者你所说的偏移是什么意思?是的,就是这样,忽略第一个x元素(你应该能够更改第一个给定的答案来跳过第一个x元素)“是的,就是这样,忽略第一个x元素”-然后你似乎误解了
offsetSet
一开始的作用。请参见此处,了解其实际功能的说明: