41

We like to store database values in array. But we do not know the maximum size of an array which is allowed in PHP?

Amruta
  • 411
  • 1
  • 4
  • 3
  • 2
    Related: [How many classes can PHP take?](http://stackoverflow.com/q/6814399/367456) – hakre Jul 28 '11 at 09:10
  • 6
    *Related:* [PHP: do arrays have a maximum size?](http://stackoverflow.com/q/3036957/367456) – hakre Nov 20 '11 at 00:15

4 Answers4

47

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.

Tim
  • 9,173
  • 1
  • 31
  • 48
17

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.

Silver Light
  • 41,576
  • 32
  • 119
  • 161
2

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

prokop
  • 21
  • 1
2

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;
    }

}