2

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

As the title says, I'm wondering what are the limits to PHP's arrays?

  1. What are the limits to array keys?
  2. Can I use any size string as the key?
  3. At what point will keys start to collide? What I mean here is, I thought PHP arrays were hashtables. My understanding of hashtables and their hash functions are that at some point two strings can hash to the same value, giving you a collision. For example MD5 was found to not be collision resistant.
  4. What is the size limit to a php array?
Community
  • 1
  • 1
Jerinaw
  • 5,260
  • 7
  • 41
  • 54
  • 3
    Even if there is a limit to the key, it's nothing you should ever reach. – Madara's Ghost Mar 16 '12 at 19:45
  • @Truth I was thinking of writing a web crawler in PHP and these questions popped into my head. I was thinking of using the URLs as keys to prevent duplicate entries and to prevent the crawler from looping on a page. – Jerinaw Mar 17 '12 at 21:36

1 Answers1

7
  1. What are the limits to array keys?

    There is no theoretical limits more then the size of memory allocated to the script. You can also find a proof at What is the max key size for an array in PHP?

    The php manual says here;

    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.

    This matters since the keys in a array can be strings.

  2. Can I use any size string as the key?

    Yes, depending on the amount of memory allocated for the script. You can set the size by starting you script with for example ini_set('memory_limit', '1024M'); this set amount of memory to one gigabyte. To set the memory limit to unlimited use -1.

    BUT when the size of the keys grows you will need more power to work with the array.

  3. At what point will keys start to collide?

    (I do not now if I understod the questions property)If you use all the combinations of letters and numbers up to a infinity long string, there will be infinity numbers of combinations. And therefor they will never collide.

  4. What is the size limit to a php array?

    It is limited in the same way as above, by the amount of memory allocated for the script.

Community
  • 1
  • 1
Mattias
  • 9,211
  • 3
  • 42
  • 42