0

我为我的网站使用 XML 导入。主要问题是执行时间和我的服务器强制执行的限制。因此,我想将 XML 导入拆分为段。到目前为止,我的脚本看起来是这样的:

$xml = simplexml_load_file('test.xml');

foreach ($xml->products as $products) {

...

}

问题是如何从特定时刻开始foreach命令,例如 foreach 可以从 100 开始。我知道可以在下面以这种方式完成,但有没有更简单的方法?

$n=0;
foreach ($xml->products as $products) {
$n++;
if ($n>99) { //do something }
else { //skip }

}
4

2 回答 2

3

只需使用 for 循环,即可指定要循环的范围

for($i = 100; $i < 200; $i++)
{
//do something
}
于 2012-12-18T10:19:09.237 回答
1

您可以使用其他人建议的或类似for的方法进行操作,或者如果它必须是,则可以使用:whilecontinueforeach

$n=0; //you have to do this outside or it won't work at all.
$min_value=100;
foreach ($xml->products as $products) {
    $n++;
    if ($n<=$min_value) { continue; } //this will exit the current iteration, check the bool in the foreach and start the next iteration if the bool is true. You don't need a else here.

    //do the rest of the code
}
于 2012-12-18T10:29:51.863 回答