3 ms·
It's not just Perl, every language I've seen or heard of except PHP has a clear distinction between its unordered key-value datatype, and an array datatype. Tha
by avar 16y ago
It's not just Perl, every language I've seen or heard of except PHP has a clear distinction between its unordered key-value datatype, and an array datatype. That includes Python, Lisp, Lua, and others.
PHP has a Frankensteinian mix between the two. You /can/ implement it in any other language by just keeping an array along with your hash that indicates the order in which elements were inserted: http://search.cpan.org/perldoc?Tie::StoredOrderHash http://search.cpan.org/perldoc?Tie::StoredOrderHash
But you almost never want to do this. I've only used it once in all the PHP code I've written. And having to deal with the ambiguity of PHP's key-value array datatype wasn't worth it elsewhere wasn't worth that.
- pornel 16y agoMix of array and hash types, combined with automatic type conversion has one very ugly gotcha: $x[1] === $x['1']. However, other than that I don't have problems with PHP's "arrays". I find ordered key-value datatype very useful in lots of situations — all kinds of mappings ordered by priority, queues where you can remove any item quickly, trees where order matters (e.g. stuff coming from XML). Sometimes even in cases where order doesn't strictly matter, preserving order makes operations more predictable and results easier to compare (e.g. you can load ini file to PHP "array", edit and save, and it won't get shuffled). It might be a matter of how you think about your data: whether you think of it as arrays and hashes and try to retrofit it in PHP, or whether you think of mapping it into PHP's peculiar "array" from the start.
- silentbicycle 16y agoLua is an interesting case, because while arrays and associative arrays (AKA dicts/hashes) are the same data type, when dicts have keys 1..N* , those values are kept in an array rather than a hash table. (For O(1) access with a lower constant factor.) * Yeah, Lua is 1-indexed. Ah well.
- scotty79 16y agoActually PHP arrays are ordered dictionaries. Python got standard implementation of this kind of collection type just recently. This 'Frankensteinian mix' is the most versatile collection type I have seen so far and I attribute large part of PHP success to having this as first choice when in need to keep collection of elements. You can build vector, list, stack, tree, map without even knowing what those are just confronting what abilities you require with what PHP arrays can do.