I am wondering is there a size limit for array in php 5? I wrote a php script to change the last element of each line in a file. print out the content of a modified file, when I run the script on a small-sized file (i.e. each line comprises 4 values), it will work. When I run it on a larger file (like 60000 lines, each of which comprises 90 values), it does not change a bit of the original file, but the script did not throw any exception message during runtime. What is this problem?
-
its not the issue of array size limit.. it is the issue of execution time limit..– xkeshavCommented May 8, 2011 at 6:07
-
6You should consider using streams rather than dumping this all into memory– MikeMurkoCommented Mar 19, 2013 at 16:15
-
1+1 MikeMurko. Don't try to read the whole file into memory, read one line at a time, make your edits to it, write it to the destination, and then move on to the next line...– siliconrockstarCommented Jan 16, 2014 at 18:45
2 Answers
You could be running out of memory, as your array size is (in theory) only limited by the amount of memory allocated to the script.
Put ini_set('memory_limit', '1024M');
in the beginning of your script to set the memory limit to 1 GB. You may need to increase this even higher for best results.
As manyxcxi says you will probably have to increase the memory_limit. Sometimes you can also reduce memory usage by unsetting large variables with unset($var). This is only needed when the variable stays in scope far past it's last point of use.
Are you by any chance now reading the whole file, tranforming it and then writing the new file? If so you can reduce memory usage by working in a loop where you read a small part, process it and write it out and repeat that until you reach the end of the file. But only if the transformation algorithm doesn't need the whole file to transform a small part.