5 ms·
# Arrays and hashes treated as the same type For me this is an advantage over other languages. I'm looking at you, Perl.
by FiReaNG3L 16y ago
# Arrays and hashes treated as the same type
For me this is an advantage over other languages. I'm looking at you, Perl.
- avar 16y agoIt'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.
- wazoox 16y agoI don't get it. An array is a different beast, and this is a major annoyance that php misses proper arrays. When you define $array[10], you summon into existence $array[0] to $array[9]. This is an intended and expected side-effect...
- randallsquared 16y agoWhat you want in PHP would be $array = range(0,10); I guess. I can't think of a time when I wanted to assign directly to a non-sequential number in a vector and have a (potentially huge) amount of space taken up "below" it; wouldn't that typically be a bug?
- wazoox 16y agoOf course this example is contrived: my @array; # $#array : number of @array elements say $#array ; # print -1 $array[10]=3; say $#array; # print 10 But it is quite common to populate an array, then delete some elements in it for instance.
- randallsquared 16y agoShouldn't your third line print zero instead of negative one? Maybe negative one is a special result of an uninitialized array in perl? Hm. Anyway, the actual use case of populating an array and then deleting some elements works fine in PHP; I don't think there's any difficulty with expressing what you want -- it's just going to be somewhat less efficient than it could be. In your example, it's not clear to me what indexes zero through nine point to... null, maybe? In PHP, you'd have to be explicit about what values you wanted in zero through nine by using array_fill(), or live with: for($i=0;$i<=10;$i++) { if(!isset($array[$i])) continue; // ... } If you use a for loop, you'd have to check each value to make sure it's defined like that. If you don't care about order in the loop, you could use foreach and avoid the check and a lot of extraneous looping.