4 ms·
I don’t believe there are maps in PHP - just plain arrays which to be honest are pretty simple to use. Unlike say JavaScript where arrays are always numericall
by jamroom 7y ago
I don’t believe there are maps in PHP - just plain arrays which to be honest are pretty simple to use. Unlike say JavaScript where arrays are always numerically indexed, PHP can use any value as an index.
- azernik 7y agoJavaScript, by contrast, doesn't have arrays - it just has objects, which can have numerical keys. a = [] a.foo = 'bar' // works! b = {} b[0] = 42 // also works! I happen to think these designs are both INSANE, but even if you like them, PHP has no advantage here.
- uryga 7y agoso isn't it like PHP, which doesn't have lists, only maps with integer keys?
- azernik 7y agoWith the exception that unlike (I think?) PHP, JS also uses maps (w hich can only have string keys [1]) to implement objects and classes. Hence their being called objects, not maps. [1] So how do arrays have numerical keys? Well... actually they don't. I was trying to make JS look better than it is in my original comment. Actually, array keys are stringified versions of numbers, and values are automatically cast to string when you put them in indexing braces. Object.keys([1, 2]) --> ["0", "1"] As you can imagine, this leads to gotchas when you assume that a JS object can have, say, a date as a key b = {}; b[new Date()] = 10; Object.keys(b) --> [ "Sat Nov 09 2019 23:15:58 GMT-0800 (Pacific Standard Time)" ]
- roblabla 7y agoI’m pretty sure this is wrong. JS objects and arrays are different. The former will only yield elements from the array itself when iterated on, and has a consistent iteration order, while the later does not.
- mncharity 7y agohttps://www.ecma-international.org/ecma-262/#sec-array-exotic-objects https://www.ecma-international.org/ecma-262/#sec-array-exoti...
- TurningCanadian 7y agoOrder is consistent in both https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/ https://www.stefanjudis.com/today-i-learned/property-order-i...
- cutler 7y agoJS arrays are array-like objects. That's the official line anyway.
- azernik 7y agoThat's because arrays happen to override the special [Symbol.iterator] method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Arrays are objects of class Array which have special literals, override the toString() method, and update the non-enumerable "length" property on certain operations. Try calling Object.keys([1, 2, 3]), or even better try Object.getOwnPropertyNames([1, 2, 3])
- uryga 7y agoi think that's just a matter of naming – afaik PHP "arrays" are what you'd call a map/dict/hash in other languages
- capn_cabbage 7y agoI believe it is just a naming issue. Not even sure that PHP devs have agreed on how to name the type. Internally, PHP calls them both arrays and hash tables if I understand correctly. In Zend/zend_types.h[1] of PHP source: typedef struct _zend_array zend_array; typedef struct _zend_array HashTable; struct _zend_array { ... }; That being said AFAICT, HashTable and zend_array are used interchangeably throughout the source. I am not a C programmer, but I did write a couple of PHP extensions and that was my general understanding. Perhaps it is a compatibility issue or just used to abstract types differently in various areas of the C API. Check out [2] for a deeper understanding of how arrays are handled internally in PHP. [1]: https://github.com/php/php-src/blob/php-7.3.11/Zend/zend_types.h https://github.com/php/php-src/blob/php-7.3.11/Zend/zend_typ... [2]: https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implem...
- igouy 7y agortm ? "An array in PHP is actually an ordered map." https://www.php.net/manual/en/language.types.array.php https://www.php.net/manual/en/language.types.array.php