如何跳过foreach循环中的元素 [英] how to skip elements in foreach loop

查看:354
本文介绍了如何跳过foreach循环中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在foreach循环中跳过一些记录。

例如,循环中有68条记录。如何跳过20条记录并从记录#21开始?

解决方案


$ b

通过array_keys进行双重寻址

for循环的问题在于键可能是字符串或不是连续的数字,因此您必须使用double寻址(或表查找,可以任意调用它),并通过它的数组访问数组。

  //初始化25个项目
$ array = range(1,25,1);

//你需要获得数组键,因为它可能是关联数组
//或者它将包含键0,1,2,5,6 ...
//如果索引从0开始连续(例如从db-> fetch_all)
//你可以忽略这个
$ keys = array_keys($ array); ($ i = 21; $ i <25; $ i ++){
echo $ array [$ keys [$ i]]
。 \\\
;
// echo $ array [$ i]。 \\\
; //连续数字键

code
$ b $ p $ $ b $
$ b

使用foreach跳过记录

我不认为这是做这件事的好方法(除非你有大的数组和切片或生成数组的键将使用大量的内存,这是68是明确不),但也许它会工作:)

  $ i = 0; 
foreach($ array as $ key => $ item){
if($ i ++< 21){
continue;
}
echo $ item。 \\\
;

$ / code>




使用数组切片获取子部分或数组

只需获取一块数组,然后在正常的foreach循环中使用它。

  $ sub = array_slice($ array,21,null,true); 
foreach($ sub as $ key => $ item){
echo $ item。 \\\
;

$ / code>




使用 next()



如果可以将内部数组指针设置为21 foreach循环与内部中断, $ array [21] 不起作用,我检查:P)你可以这样做(如果数组< ($($ row = next($ row $($($ row)$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' $ array))!== false){
echo $ row;

$ / code $ / pre


$我最喜欢hakre的答案。 / p>




使用

  //初始化数组迭代器
$ obj = new ArrayIterator($ array);
$ obj-> seek(21); ($ obj-> valid()){//我们现在是否有有效的偏移量
echo $ obj-> current()。 \\\
;
$ obj-> next(); //切换到下一个对象
}


I want to skip some records in a foreach loop.

For example, there are 68 records in the loop. How can I skip 20 records and start from record #21?

解决方案

Five solutions come to mind:

Double addressing via array_keys

The problem with for loops is that the keys may be strings or not continues numbers therefore you must use "double addressing" (or "table lookup", call it whatever you want) and access the array via an array of it's keys.

// Initialize 25 items
$array = range( 1, 25, 1);

// You need to get array keys because it may be associative array
// Or it it will contain keys 0,1,2,5,6...
// If you have indexes staring from zero and continuous (eg. from db->fetch_all)
// you can just omit this
$keys = array_keys($array);
for( $i = 21; $i < 25; $i++){
    echo $array[ $keys[ $i]] . "\n";
    // echo $array[$i] . "\n"; // with continuous numeric keys
}


Skipping records with foreach

I don't believe that this is a good way to do this (except the case that you have LARGE arrays and slicing it or generating array of keys would use large amount of memory, which 68 is definitively not), but maybe it'll work: :)

$i = 0;
foreach( $array as $key => $item){
    if( $i++ < 21){
        continue;
    }
    echo $item . "\n";
}


Using array slice to get sub part or array

Just get piece of array and use it in normal foreach loop.

$sub = array_slice( $array, 21, null, true);
foreach( $sub as $key => $item){
    echo $item . "\n";
}


Using next()

If you could set up internal array pointer to 21 (let's say in previous foreach loop with break inside, $array[21] doesn't work, I've checked :P) you could do this (won't work if data in array === false):

while( ($row = next( $array)) !== false){
  echo $row;
}

btw: I like hakre's answer most.


Using ArrayIterator

Probably studying documentation is the best comment for this one.

// Initialize array iterator
$obj = new ArrayIterator( $array);
$obj->seek(21); // Set to right position
while( $obj->valid()){ // Whether we do have valid offset right now
    echo $obj->current() . "\n";
    $obj->next(); // Switch to next object
}

这篇关于如何跳过foreach循环中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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