PHP: do arrays have a maximum size?

前端 未结 2 1571
渐次进展
渐次进展 2020-11-30 08:39

Is there a limit for an array in PHP?

2条回答
  •  孤独总比滥情好
    2020-11-30 09:20

    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.

提交回复
热议问题