3 ms·
How is this different from Python's lists/dictionaries? Also IIRC PHP does internally differenciate between array(1,2,3) and array('blah'=>1, 'bloop' =>); Just
by antihero 15y ago
How is this different from Python's lists/dictionaries?
Also IIRC PHP does internally differenciate between array(1,2,3) and array('blah'=>1, 'bloop' =>); Just the syntax is not as clear.
- pavel_lishin 15y agoWhat it does internally isn't much compensation for the fact that the code I write becomes harder to work with. :(
- masklinn 15y ago> How is this different from Python's lists/dictionaries? It's not. FWIW, I know of three languages which use the same data structure for arrays and associative arrays (/maps/hashes/whatever key/value collections are called these days): PHP, Lua and Awk (edit: had originally put "Tcl" here, but an other commenter set me straight: Tcl's arrays are called "list", Tcl's associative arrays are called "array", this got me confused. Sorry about that). Some functional languages also use lists of (key, value) pairs for small associative arrays (they may or may not provide "proper" associative arrays as well). They are called "association lists"[0] in many lisps (using a list of dotted pairs, rather than a list of lists) and "proplists"[1] in Erlang (using a mixed list of tuples and atoms). [0] http://www.gnu.org/s/emacs/manual/html_node/elisp/Association-Lists.html http://www.gnu.org/s/emacs/manual/html_node/elisp/Associatio... [1] http://www.erlang.org/doc/man/proplists.html http://www.erlang.org/doc/man/proplists.html
- beambot 15y agoFrom what I recall, Python (2.6) dictionaries do not necessarily preserve the order of items in the underlying structure. Eg. if "x = {1:1, 2:2, -1:5}", then x.items() will not necessaryily return "[(1, 1), (2, 2), (-1, 5)]" However, in Python 2.7+ ordered dictionaries in the collections library do preserve order. Perhaps this achieves the desired behavior?
- danudey 15y agoThe syntax is unclear, but also the behaviour is unclear. $var[] = "entry1"; $var['something'] = "entry2"; $var[] = "entry3"; $var[0] is now "entry1", and $var["something"] is "entry2", but what's the index for entry3? And if you iterate over the array, will you get entry2 before entry3? What if you create $var[$somestring], but $somestring is "29"? Now it will be at $var[29], and automatic indexing will reset to 30, which you may not expect. There's a lot of argument to "just don't write code like that", but with the inconsistencies in PHP library functions (and PHP's careless type conversion) it's easy to come across cases where this sort of "do something unexpected instead of throwing an error" behaviour can bite people in the ass down the road, through no fault of their own.
- antihero 15y agoOh yes, I don't deny that it's horribly done. I use my own classes "Dict" and "Li" instead of arrays whenever I need them to be specific, generally.