5 ms·
An “array” in PHP is an ordered map.
by mfonda 4mo ago
An “array” in PHP is an ordered map.
- pavel_lishin 4mo agoIsn't exactly their complaint? It's called an array, referred to consistently everywhere as an array, but it just ... isn't.
- yegle 4mo agoApparently array is short for associated array:-)
- deleted 4mo ago[deleted]
- hparadiz 4mo agoIn PHP the array index supports different key types and there's various optimizations that need to happen when the indexes are all numeric, mixed, or all strings or anything else. Technically it's called associative as soon as even one of the indexes isn't numeric. Internally though they are always numeric and anything non integer is hashed internally with DJB2 (Daniel J. Bernstein) hash algorithm and then stored. Using a non numeric index is slightly slower for that reason.
- thayne 4mo agoBut even if you only have numeric keys, those keys don't need to be consecutive, or start at 0.
- postalrat 4mo agoBetter than calling it a hash.
- pavel_lishin 4mo agoI don't think it is, tbh. Perl's hashes are a complete mystery to me still, but at least it lets me know that it's not just a linear, uh, well, array.
- duskwuff 4mo ago> Perl's hashes are a complete mystery to me still They're unordered mappings from strings to arbitrary values ("scalars" in Perl jargon). In this sense they're just like an object in JavaScript. Where this gets a little weird is that Perl arrays and hashes are fundamental types distinct from scalars - you can't put a hash into a $variable without taking a reference to it first, for instance. But that's more a matter of Perl being picker about the value/reference distinction than a hash-specific thing.
- johannes1234321 4mo agoPHP arrays are vectors, hash maps and doubly linked lists in one
- postalrat 4mo agoMy issue is a ton of people call all maps a hash regardless how it's implemented.
- johannes1234321 4mo agoWith good abstraction the implementation can be swapped without many noticing. However PHP leaks different aspects hear and there, making this harder ...