我基本上需要一些foreach循環內的東西,它會跳過數組的前10次迭代。讓foreach跳過迭代
foreach($aSubs as $aSub){
if($iStart > '0')
//Skip first $iStart iterations. Start at the next one
}
感謝
我基本上需要一些foreach循環內的東西,它會跳過數組的前10次迭代。讓foreach跳過迭代
foreach($aSubs as $aSub){
if($iStart > '0')
//Skip first $iStart iterations. Start at the next one
}
感謝
啓動計數器,並使用continue
跳過前十次的循環:
$counter = 0 ;
foreach($aSubs as $aSub) {
if($counter++ < 10) continue ;
// Loop code
}
使用迭代器:
$a = array('a','b','c','d');
$skip = 2;
foreach (new LimitIterator(new ArrayIterator($a), $skip) as $e)
{
echo "$e\n";
}
輸出:
c
d
或者使用索引(如果陣列具有從0整數密鑰.. N-1):
foreach ($a as $i => $e)
{
if ($i < $skip) continue;
echo "$e\n";
}
如果$ ASUBS是未實現迭代一個類的一個對象,和該指數是連續的整數(從零開始),它會更容易:
$count = count($aSubs);
for($i = 9, $i < $count; $i++) {
// todo
}
其實,你並不需要使用foreach
迴路L1的優勢來聲明另一個變量$counter
關鍵字:
foreach ($aSubs as $index => $aSub) {
if ($index < 10) continue;
// Do your code here
}
這比在foreach循環外聲明另一個變量更好。
完美的作品。真是個好主意。謝謝 – tmartin314 2010-10-02 22:36:37