PHP中数组的最大密钥大小是多less?

我正在生成关联数组,键值是1..n列的stringconcat。

有没有最大长度的钥匙,会回来咬我? 如果是这样,我可能会停下来,做不同的事情。

似乎只受限于脚本的内存限制。

快速testing让我有一个128MB的关键没有问题:

ini_set('memory_limit', '1024M'); $key = str_repeat('x', 1024 * 1024 * 128); $foo = array($key => $key); echo strlen(key($foo)) . "<br>"; echo strlen($foo[$key]) . "<br>"; 

PHP中的string大小没有实际的限制。 根据手册 :

注意:string变得很大是没有问题的。 PHP对string的大小没有限制; 唯一的限制是运行PHP的计算机的可用内存。

假设这将适用于在数组中使用string作为键也是安全的,但根据PHP处理查找的方式,当string变大时,您可能会注意到性能受到影响。

在zend_hash.h中,你可以findzend_inline_hash_func()方法,它可以显示如何在PHP中散列密钥string,所以使用小于8个字符的string长度对性能更好。

 static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength) { register ulong hash = 5381; /* variant with the hash unrolled eight times */ for (; nKeyLength >= 8; nKeyLength -= 8) { hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; } switch (nKeyLength) { case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 1: hash = ((hash << 5) + hash) + *arKey++; break; case 0: break; EMPTY_SWITCH_DEFAULT_CASE() } return hash; }