12

If there are some kind of limitations for array keys in PHP ? Length ? Not acceptable strings ?

In the official documentation found only this, but there is no information about keys limitations.

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.*

rid
  • 61,078
  • 31
  • 152
  • 193
Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68

3 Answers3

11

Any string used as a key in an array is hashed. Analogous to md5() and sha1() this hashing reduces (potentially gigabytes of) characters to a known length. unlike md5() or sha1() the array's internal hashing mechanism will convert your string to an integer it can then use to address a bucket within the array. PHP's arrays aren't true/real arrays - they are some sort of Linked HashMap internally. Considering that multiple strings can boild down to the same hash, each bucket is a list itself. If there are multiple elements within the same bucket, each key has to be evaluated. It goes without saying that short keys are compared faster than 1MB of text.

TL;DR: although you are not limited by PHP, you should limit yourself. If you have fairly long strings, consider running them through md5() or sha1() (or any other hashing function, actually) to reduce the key length.

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Didn't knew, what the key is hashed internally, thanks for the explication. Indeed, it sounds good idea to make an additional md5 to reduce the key length. – Fedir RYKHTIK Feb 29 '12 at 09:50
  • 4
    Don't you kind of contraddict yourself? If strings are hashed internally, why do should you hash "fairly long" string at all? Not to mention you would loose the theoretical guarantee of unicity (hence what do you do? do you reinvent the whole data structure, with lists etc?) Your TL;DR part seems to contraddict the first paragraph and I don't see any reasoning behind it. – matteo Feb 29 '16 at 16:08
8

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

This question is almost the exact same. But if you dont want to trust anything unofficial, just stick to using less small keys. You may even get some performance benefits out of it.

EDIT: And as the The PHP Manual says:

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..

Community
  • 1
  • 1
MichaelH
  • 1,600
  • 3
  • 14
  • 20
  • Sounds great. I need long keys for some specific data treatment, so there is no question of performance, just functionality. Need to check if there is no "forbidden" characters too, it's the second part of the question. – Fedir RYKHTIK Feb 29 '12 at 09:52
  • 2
    Strings do not have forbidden characters. However be aware of the backslash in strings / as it will escape the next letter. – MichaelH Feb 29 '12 at 10:04
  • 1
    Minus one for quoting the "no problem for long strings" note in PHP docs. That doesn't necessarily mean that long keys in an array aren't an issue, for performance or for other reasons. That may be true, but that quoted note doesn't say that. – matteo Feb 29 '16 at 16:04
  • I did some performance tests against PHP 5.3, which showed that key length definitely impacted on performance: https://gist.github.com/MarkMaldaba/a6f44f1da32080328b11. I haven't retested on later PHP versions, but the code is there if you want to have a go. – HappyDog Jul 08 '19 at 13:32
-2

are you sure you're refering to the key? or do you mean value?

with associative arrays : $array = new array( new array( "key"=>"value" ) );

.. as for the key i think in theorie there's no limitations to length however .. chosing long keys isn't a good idea if you'll want to reusre them a lot..

as for the values you should just take a loot at arrays in general and what datatypes are allowed and stuff..

hope this helps..