Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 如何从特定时刻开始执行每个命令?_Php_Xml_Foreach - Fatal编程技术网

Php 如何从特定时刻开始执行每个命令?

Php 如何从特定时刻开始执行每个命令?,php,xml,foreach,Php,Xml,Foreach,我的网站使用XML导入。主要问题是执行时间和服务器强制执行的限制。因此,我想将XML导入拆分为多个段。到目前为止,我的脚本看起来是这样的: $xml = simplexml_load_file('test.xml'); foreach ($xml->products as $products) { ... } 问题是如何从特定时刻开始执行foreach命令,例如,foreach可以从100开始。我知道下面的方法可以做到,但有没有更简单的方法 $n=0; foreach ($xml-

我的网站使用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 }

}

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

for($i = 100; $i < 200; $i++)
{
//do something
}
($i=100;$i<200;$i++)的

{
//做点什么
}

只要使用for循环,就可以指定要循环的范围

for($i = 100; $i < 200; $i++)
{
//do something
}
($i=100;$i<200;$i++)的

{
//做点什么
}

您可以使用
for
进行操作,而
与其他人建议的一样,您也可以使用
continue
(如果必须是
foreach
):

$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
}
$n=0//你必须在外面做,否则根本不行。
$min_值=100;
foreach($xml->products as$products){
$n++;

如果($n您可以使用
for
while
像其他人建议的那样进行操作,或者如果必须使用
foreach
,您可以使用
continue

$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
}
$n=0;//您必须在外部执行此操作,否则根本无法工作。
$min_值=100;
foreach($xml->products as$products){
$n++;

如果($ncheck Generators in PHP5.5 check Generators in PHP5.5这是一个不错的主意,从100到200,但是我想从100到$xml->products中所有值的末尾。所以有一种方法可以知道有多少值有$xml->products?因为例如,如果$xml->products以110结尾,那么90次循环将为空。是的,您可以将其设置为
($i=100;$iproducts);$i++)
谢谢@Naryl-现在答案已经完成了。这正是我想要的。这是个好主意,从100到200,但我想从100到$xml->products中所有值的末尾。所以有一种方法可以知道有多少值有$xml->products?因为例如,如果$xml->products以110结尾,那么90次循环将是空的。是的,你可以这样做
for($i=100;$iproducts);$i++)
谢谢@Naryl-现在答案已经完成了。这就是我一直在寻找的。谢谢你的这个想法,“continue”命令以后可能会对我有用。我想“for”和“count”一起使用正如您前面提到的,对于我的php脚本来说,continue更好。
continue
通常更有用,因为您希望跳过遵循顺序的其他条件的特定元素,例如id等于某个值的元素,或者元素为null的元素,等等。由于这个想法,“continue”命令稍后可能对我有用。我认为“for”对于我的php脚本来说,使用前面提到的“count”会更好。
continue
通常更有用,因为您希望跳过遵循其他条件的特定元素,例如id等于值的元素,或者元素为null的元素,等等。