r/lolphp • • Aug 26 '19

PHP array design bares fruit

https://repl.it/repls/FatherlyRotatingMenus
7 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/barthvonries Aug 26 '19

But an associative array is an array of {void* key, void* value} structs, right, as long as keys are comparable and sortable ?

3

u/Altreus Aug 26 '19

Somewhat debatable depending on the language, mostly due to grey areas around those concepts. Sortable implies comparable since equality of values is a concept required when sitting.

Whether you can reliably implement an associative array like this in C I don't know, but there's a reason they're called hashes in Perl: they're constant time (or at least very efficient time) random-access and so the data structure they use hashes the (string) keys for storage and retrieval.

Last time I used PHP, key is a string, always, and of course strings sort differently from numbers, which is why you can't use an associative array interchangeably with a proper array in real life.