Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array size limit in PHP

Tags:

arrays

php

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?

like image 206
Michael Avatar asked May 08 '11 05:05

Michael


People also ask

Is there a limit to array size?

The theoretical maximum Java array size is 2,147,483,647 elements.

What is the size of array in PHP?

How to Count all Elements or Values in an Array in PHP. We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that we can initialize with an empty array.

What limits the maximum size of an array?

Without regard for memory, the maximum size of an array is limited by the type of integer used to index the array. When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand (C11 n1570, section 6.5.


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.

like image 61
ashurexm Avatar answered Sep 21 '22 05:09

ashurexm


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.

like image 35
Eelke Avatar answered Sep 22 '22 05:09

Eelke