On 6/3/2017 1:31 AM, Pawel Por wrote:
Hi,

AFAIK the hashtable in PHP by default keeps the order in which the
elements are inserted. Could you please write me why it keeps the
insertion order by default ?

Thanks in advance for clarification.

Because it is a brilliant, high performance solution to extremely annoying classical data structures (aka unordered hashes) found in other programming and scripting languages. It's the only sane option to present to any language's users and the cost is minimal. PHP nailed it very early on and the PHP array implementation is the primary reason I use PHP today. If you want pseudorandom-ish order back in your userland PHP arrays, then you could try using array_rand() or similar.

Keys can also be a mixture of integers and strings. Most hash table implementations require choosing one key type and sticking with that.

The only weakness in PHP's implementation is that there aren't built-in userland functions to insert elements before/after specific keys. Users instead have to rebuild the whole array just to insert one element at a desired position while maintaining keys and order. It's occasionally useful to do so and rebuilding an array can be an expensive memory-churning operation when a much more performant solution is just slightly out of reach of userland.

From:

https://github.com/cubiclesoft/cross-platform-cpp

"The detachable node ordered hash is similar to PHP arrays. It accepts both integer and string keys in the same hash, has almost constant time insert (anywhere!), lookup, delete, and iteration operations (both directions), and, most importantly, maintains the desired order of elements."

All the potential benefits of marrying a hash table to a linked list in a single paragraph.

--
Thomas Hruska
CubicleSoft President

I've got great, time saving software that you will find useful.

http://cubiclesoft.com/

And once you find my software useful:

http://cubiclesoft.com/donate/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to