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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/467149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:50:44  来源:igfitidea点击:

What is the max key size for an array in PHP?

phparrayskey

提问by Ross

I am generating associative arrays and the key value is a string concat of 1..n columns.

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

Is there a max length for keys that will come back to bite me? If so, I'll probably stop and do it differently.

会回来咬我的钥匙是否有最大长度?如果是这样,我可能会停下来并以不同的方式做。

回答by Greg

It seems to be limited only by the script's memory limit.

它似乎仅受脚本内存限制的限制。

A quick test got me a key of 128mb no problem:

快速测试让我得到了一个 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>";

回答by Lusid

There is no practical limit to string size in PHP. According to the manual:

PHP 中的字符串大小没有实际限制。根据手册

Note: It is no problem for a string to become very large. PHP imposes no boundary on the size of a string; the only limit is the available memory of the computer on which PHP is running.

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

It is safe to assume that this would apply to using strings as keys in arrays as well, but depending on how PHP handles its lookups, you may notice a performance hit as strings get larger.

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

回答by Bob Bao

In zend_hash.h, you can find zend_inline_hash_func()method that can show how to hash key string in PHP, So use key which string length less than 8 characters is better for performance.

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