70

連想配列を生成しています。キー値は1..n列の文字列連結です。

私を噛むために戻ってくるキーの最大長はありますか?もしそうなら、私はおそらく停止し、別の方法でそれを行います。

4

3 に答える 3

90

スクリプトのメモリ制限によってのみ制限されるようです。

簡単なテストでは、問題なく 128 MB のキーが得られました。

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>";
于 2009-01-21T21:55:07.607 に答える
17

PHPの文字列サイズに実際的な制限はありません。マニュアルによると:

注:文字列が非常に大きくなっても問題ありません。PHPは、文字列のサイズに境界を課しません。唯一の制限は、PHPが実行されているコンピューターの使用可能なメモリーです。

これは、配列のキーとして文字列を使用する場合にも当てはまると考えるのが安全ですが、PHPがルックアップを処理する方法によっては、文字列が大きくなるにつれてパフォーマンスが低下する場合があります。

于 2009-01-21T21:53:08.953 に答える
6

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;   
}
于 2014-03-26T02:54:17.867 に答える