I load data from a huge XML-File into an array. That's fine for now, is the memory of the web server not overloaded. But as soon as I try to iterate through that array to work with the data, then I get a memory error. Setting memory-limit to -1 is not an option, obviously.
So, what I am trying to do, is iterating through the array step by step. Like the first 1,000 entries and then the next till there are no more entries left. Like this:
$i = 0;
foreach($entries as $entry) {
// Do some stuff with the entries
if ( $i == 1000) break;
}
I now want to iterate through the next 1.000 entries. My idea was to address the key of the array. Like doing the loop again but from entries[1000]. But I can't get the break from the loop and going back into the loop again going.
Is, what I try to achieve, even possible?
Thank you very much!
CodePudding user response:
The issue probably happens before your foreach loop is reached. You should take a look at the code parses the XML file and populates the array $entries
. If you are using XMLParser
then you should look at the handler functions (they are usually called startElements
and endElements
, if not look for xml_set_element_handler
and the function names would be in its second and third parameters). In the handler functions, you are probably populating the array $entries
. You should do your processing directly in the handlers and retrieve the relevant data w/o putting everything in the memory.
PS: If my theory turns out to be correct, you should change the title and rephrase the question
CodePudding user response:
Try this if your data is really Big and slow your web server:
for($i = 1;$i <= count($entries);$i ) {
if($i % 1000 === 0) {
// your code here
}
usleep(100000); // delays execution for 0.1 second x 1000 entries
}
CodePudding user response:
from what you did I think you should have done this instead
for ($i=0; $i>=1000; $i ){
}
Note: the $i=0
is when you want it to start counting from zero but if you want it to count from 1 then replace the 0 with 1