PHP中数组的最大键大小是多少?

PHP中数组的最大键大小是多少?,php,arrays,key,Php,Arrays,Key,我正在生成关联数组,键值是1..n列的字符串concat 有没有一个最大长度的钥匙会回来咬我?如果是这样的话,我可能会停下来换一种方式来做。在PHP中,字符串大小没有实际限制。根据: 注意:使用字符串是没有问题的 变得非常大。PHP不施加任何限制 字符串大小的边界;这个 唯一的限制是的可用内存 运行PHP的计算机 可以安全地假设,这也适用于将字符串用作数组中的键,但根据PHP处理其查找的方式,您可能会注意到字符串变大时性能会受到影响。这似乎只受脚本内存限制 通过快速测试,我得到了一个128mb的

我正在生成关联数组,键值是1..n列的字符串concat


有没有一个最大长度的钥匙会回来咬我?如果是这样的话,我可能会停下来换一种方式来做。

在PHP中,字符串大小没有实际限制。根据:

注意:使用字符串是没有问题的 变得非常大。PHP不施加任何限制 字符串大小的边界;这个 唯一的限制是的可用内存 运行PHP的计算机


可以安全地假设,这也适用于将字符串用作数组中的键,但根据PHP处理其查找的方式,您可能会注意到字符串变大时性能会受到影响。

这似乎只受脚本内存限制

通过快速测试,我得到了一个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>";
ini_集('memory_limit','1024M');
$key=str_repeat('x',1024*1024*128);
$foo=数组($key=>$key);
echo strlen(钥匙($foo))。“
”; echo strlen($foo[$key])。“
”;
在zend_hash.h中,您可以找到
zend_inline_hash_func()
方法,该方法可以显示如何在PHP中哈希键字符串,因此使用字符串长度小于8个字符的键对性能更有利

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;   
}
静态内联ulong zend\u inline\u hash\u func(char*arKey,uint nKeyLength){
寄存器ulong hash=5381;
/*散列展开8次的变量*/
对于(;nKeyLength>=8;nKeyLength-=8){

散列=((RoBorg,如果我有一个超过128mb的密钥,我可能会在每日WTF上找到自己。非常感谢。yikes!我当然不必担心我的密钥会稍微超过255个字符。请记住,PHP可能不是密钥大小的唯一限制因素。例如,
memcache
可能会截断$\u会话中的密钥如果它们太长。