I use XML import for my website. The main problem is the execution time and the limit enforced by my server. Because of that I want to split XML import into segments. My script looks so far in this way:
$xml = simplexml_load_file('test.xml');
foreach ($xml->products as $products) {
...
}
The question is how can I start foreach command from specific moment, e.g. foreach could start from 100. I know it can be done in this way below, but is there any easier way?
$n=0;
foreach ($xml->products as $products) {
$n++;
if ($n>99) { //do something }
else { //skip }
}
Just use a for loop, than you can specify the range you want to loop over
for($i = 100; $i < 200; $i++)
{
//do something
}
you can do it with a for
or a while
like other people suggested or you can use continue
if it MUST be a 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
}