Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: do arrays have a maximum size?

Tags:

arrays

php

Is there a limit for an array in PHP?

like image 299
Febin Joy Avatar asked Jun 14 '10 11:06

Febin Joy


People also ask

Is there a limit to array size?

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

How many dimensions can there be in an array PHP?

Multidimensional PHP arrays can have any number of dimensions, starting from two. Two-dimensional PHP arrays contain arrays in which variables are stored.

What happens if you exceed the size of an array?

Explanation: If the index of the array size is exceeded, the program will crash.

Does an array have a fixed length?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.


2 Answers

Yes, there's a limit on the maximum number of elements. The hash table structure (arrays are basically wrappers around a hash table) is defined like this (PHP 5.3):

typedef struct _hashtable {     uint nTableSize;     uint nTableMask;     uint nNumOfElements;     ulong nNextFreeElement;     Bucket *pInternalPointer;   /* Used for element traversal */     Bucket *pListHead;     Bucket *pListTail;     Bucket **arBuckets;     dtor_func_t pDestructor;     zend_bool persistent;     unsigned char nApplyCount;     zend_bool bApplyProtection; #if ZEND_DEBUG     int inconsistent; #endif } HashTable; 

given that

typedef unsigned int uint; 

the limit is the maximum size of an unsigned int (typically 2^32-1 on a 32-bit OS and on most 64-bit OS).

In practice, however, except on machines with lots of RAM and 32-bit ints, you will always hit the memory limit before this becomes an issue.

like image 198
Artefacto Avatar answered Sep 30 '22 09:09

Artefacto


The only thing I've come across in reference to php is this from bytes.com/forum:

I don't think there is a limit on how big an array can be, but there is a limit on how much memory your script can use.

The 'memory_limit' directive in the php.ini configuration file holds the max amount of memory your scripts can consume. Try changing this, see if that helps.

like image 37
ChrisF Avatar answered Sep 30 '22 07:09

ChrisF