On Thu, 22 Mar 2012 03:22:10 -0700, Dmitriy Sintsov questpc@rambler.ru wrote:
- Daniel Friesen lists@nadir-seen-fire.com [Thu, 22 Mar 2012 01:40:16
-0700]:
That's the very definition of an array. An array is a list, the keys
are
indexes. By definition you cannot have an index that does not exist.
If PHP arrays are sparse, including numeric ones. They are actually like hashmaps I think so. I don't know whether they use different ways to store numeric and string keys. I haven't studied the low-level implementation. They also have "real" arrays like in C (lower level), however these are the part of SPL: http://php.net/manual/en/class.splfixedarray.php
PHP arrays are not arrays at all. PHP's array() is a bastardized fusion of arrays and hash tables. But they insist on calling them arrays despite the hard fact that they are completely different from arrays in every other language.
PHP's implementation of an array is in zend_hash.c it's a HashTable with ordered keys. I think I see some linked-list like code inside there that handles the ordering.
When you use $x[0] in an array you are really looking up the value for the key "0". Hence you can actually have an "array" where 3 comes before 1. There's no real way to access the actual array indexes of a php 'array' besides when you use things like reset, next, array_splice, etc...
an item in the array does not exist then the items after it take on
earlier
indexes and the length is shorter. If you have a case where 0 and 2 exists but 1 is not supposed to exist then you're not using an array, you're using a hashtable, map, etc... For that purpose you should be using Object like a hashtable: var h = {} h[0] = 'a'; h[2] = 'c';
You can use for..in on such a thing. And you also shouldn't have to worry about using hasOwnProperty on it. If anyone is stupid enough to set something on Object.prototype a lot more than your script will break.
Ok. Which are the cases when jQuery.each() is preferable? I use it with jQuery selectors. Or, something else as well? Dmitriy
$.each is useful for arrays when you don't want to write a full for ( var i ... blah blah or need a function scope to make closures work.
$.each is only useful for objects if you want a function scope for closures.