What is the maximum size of an array in PHP?
We like to store da开发者_如何学Pythontabase values in array. But we do not know the maximum size of an array which is allowed in PHP?
There is no max on the limit of an array. There is a limit on the amount of memory your script can use. This can be changed in the 'memory_limit' in your php.ini configuration.
Array size is limited only by amount of memory your server has. If your array gets too big, you will get "out of memory" error.
It seems to me to be the 16-bit signed integer limit. (2^15)
$ar = [];
while (array_push($ar, null)) {
print 'a';
}
Length of output: 32768
If, like me, you need to use a huge array in a class in PHP 5.6.40 and have found that there is a limit to the size of class arrays so that they get overflowed and overwritten when surpassing 32768 elements, then here is the solution I found to work.
Create a public function with the huge array in it as a local variable. Then assign that local variable to the class variable. Call this function right in the constructor. You will see that it prints the correct size of the array instead of the overflow leftover size.
class Zipcode_search {
public $zipcodes;
public function __construct() {
$this->setHugeArray();
print "size is ".sizeof($this->zipcodes). "<br />";
}
public function setHugeArray(){
$zipcodes=[too much stuff];//actual array with +40,000 elements etc.
$this->zipcodes = $zipcodes;
}
}
2,147,483,647 items, even on 64-bit PHP. (PHP 7.2.24)
In PHP, typedef struct _hashtable
is defined with uint
values for nTableSize
and nNumOfElements
.
Because of this, the largest array you can create with array_fill()
or range()
appears to be 2^32-1
items. While keys can be anything, including numbers outside that range, if you start at zero, with a step size of 1, your highest index can be 2147483646.
If you are asking this question, you have likely seen an error like:
# php -r 'array_fill(0, 2147483648, 0);'
PHP Warning: array_fill(): Too many elements in Command line code on line 1
or even:
# php -r 'array_fill(0, 2147483647, 0);'
Segmentation fault (core dumped)
...or, most likely, the error which explicitly refers to the "maximum array size":
php -r 'range(0,2147483647);'
PHP Warning: range(): The supplied range exceeds the maximum array size:
start=0 end=2147483647 in Command line code on line 1
A caution for those reading this question:
The most common place you'll run into this, is through misuse/abuse of the range()
operator, as if it was an iterator. It is in other languages, but in PHP it is not: it is an array-filling operator, just like array_fill()
.
So odds are good that you can avoid the array use entirely. It is unsafe to do things like:
foreach (range($min, $max, $step)) { ... stuff ... }
Instead do:
for ($i = $min; $i <= $max; $i += $step) { ... stuff ... }
Equally, I've seen people doing:
// Takes 3 minutes with $min=0, $max=1e9, $step=1.
// Segfaults with slightly larger ranges.
$numItems = count(range($min, $max, $step));
Which can instead be rewritten in a more secure, idiomatic and performant way:
// Takes microseconds with $min=0, $max=1e9, $step=1.
// Can handle vastly larger numbers, too.
$numItems = ($max - $min) % $step;
If you are running into errors about array size, odds are good that you are doing crazy stuff that you probably should avoid.
精彩评论