5 ms·
> PHP really got this one right (and immediately messed it up by mixing it with arrays, but hey, PHP). No, it didnt. What PHP calls "array" is actually an Orde
by svnpenn 7y ago
> PHP really got this one right (and immediately messed it up by mixing it with arrays, but hey, PHP).
No, it didnt. What PHP calls "array" is actually an Ordered Map, as you alluded to:
https://yaml.org/type/omap https://yaml.org/type/omap
it literally says that in the documentation, paragraph one, sentence one:
> An array in PHP is actually an ordered map.
https://php.net/types.array https://php.net/types.array
The issue, if one exists, is that PHP doesnt have a sequence type:
https://yaml.org/type/seq https://yaml.org/type/seq
but one is provided as a package:
https://php.net/class.ds-sequence https://php.net/class.ds-sequence
https://github.com/php-ds/extension https://github.com/php-ds/extension
- skrebbel 7y agoSure, PHP arrays are actually ordered maps, but it's super confusing when used as arrays. I mean, look at code like this: $arr = []; $arr[2] = "two"; $arr[0] = "zero"; $arr[1] = "one"; echo join($arr, ", "); // two, zero, one In any other language, the result of similar code would be "zero, one, two". You'd need to `ksort` this thing to get it to behave like a normal bog-standard boring array. Everybody's writing PHP code pretending that it has arrays when really, it does not. The gotchas are way bigger than the gotchas in Python <3.7's unordered maps. It's a mess.
- kingdomcome50 7y agoI disagree. The behavior you exhibit makes more sense than the behavior you imagine. You start with an _empty_ array, then (somehow) set the third element of that array? That makes very little sense. Had you _initialized_ your example as an array with a length of 3, your desired behavior would, indeed, manifest.
- cliffy 7y agoThe problem is PHP calling an ordered map an array. Array in pretty much every other language means a sequence indexed by integers. Ideally the maintainers of PHP would rename it and deprecate the use of `array()` over a long period of time.
- zeroimpl 7y agoThe PHP documentation refers to them as "associative arrays", which is technically correct, but I agree I'm not sure how they could have conflated these concepts so badly.
- tyingq 7y agoI believe the "associative array" terminology came from Perl. Though Perl's associative arrays don't have any guaranteed order. And Perl also has normal arrays.
- mattkrause 7y agoIn that situation, Matlab allocates an array of length 3, fills it with “empty” values (depending on the type), and then sets the third element to the value. That’s what I’d have expected to happen here too...
- zeroimpl 7y agoWould you expect it to do that if somebody wrote the following? $id = 12835151; $arr[$id] = Get_thing_with_id( $id );
- jhasse 7y agoyes.
- jrwr 7y agoIn my bog standard PHP I would do $thing = Get_thing_with_id( $id ); if(!empty($thing)){process($thing);}
- mattkrause 7y agoIf $arr were an array I would, though I'd cringe at the wasted space (unless it were a sparse array). Obviously, I'd agree that a map/associative array/dict/etc is a better choice here; I'm just annoyed about the name.
- coldtea 7y ago>You start with an _empty_ array, then (somehow) set the third element of that array? That makes very little sense. Well, there's no reason a language could not initialize an empty array to some initial capacity, either on declaration or when you add an indexed element.
- kingdomcome50 7y agoI guess my reasoning is that: $arr = [] doesn't appear to be initializing to any capacity. So that after: $arr[2] = "two" I would expect: count($arr) === 1 And honestly, sure. A language could be designed to pad the array with empty values if you specify an index greater than the upper bound (or lesser than the lower bound). I believe a sibling mentioned matlab did something like that. But is that really better behavior or just different behavior? I don't really see an important difference. My point was that the criticism was fairly weak. PHP, indeed, has a very useful abstraction in its concept of "array". Most of the hate should be directed at the name, not the construct.
- tylerjwilk00 7y agoI have to disagree also. The order inserted makes most sense in most cases. Hand crafting integer based array indexes out of order is rare. And as you say a simple ksort call guarantees sort order. ksort($arr); echo join($arr, ", "); // one, two, three
- dpacmittal 7y agoExactly. Of you hand craft an array you probably need it in that order.
- tylerjwilk00 7y agoThen why not put it in order while you're hand crafting it?
- cdelsolar 7y agoI literally just learned "bog-standard" 10 minutes ago and now I see it here. And no, don't Baader-Meinhof me, I would have noticed this weird term in random text in the past.
- coldtea 7y ago>I would have noticed this weird term in random text in the past. Well, no so weird, I'm not a native english speaker, and I've seen the term hundreds of times over the years... It's also quite common on HN: https://www.google.com/search?q=site%3Anews.ycombinator.com+bog+standard&oq=site%3Anews.ycombinator.com+bog+standard https://www.google.com/search?q=site%3Anews.ycombinator.com+...
- viraptor 7y agoWould you though? Appears at least once a week: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=bog-standard&sort=byDate&type=all https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
- zepto 7y agoBaader-Meinhof denial!
- sblom 7y agoBaader-Meinhof Dunning-Kruger?
- perl4ever 7y agoI'm aware of the terms "bog" and "bog-standard", but not being from the UK, I'm not sure what is so standard about bogs.
- nedt 7y agoWhy wouldn‘t you just push/append to the end of the array with '$arr[]'? There is no need for explicit keys in PHP. The gotcha is writing code in PHP pretending you are using a different language.
- slim 7y agoSo how can you access, say, the third element of your ordered map in PHP ?
- skrebbel 7y agoarray_values($arr)[2];
- svnpenn 7y agoI mean youd probably have to iterate. Either that or use a package with proper array support.
- slim 7y agoThat's messed up
- mywittyname 7y agoIn practice, it's almost never an issue. The standard lib has push/pop/shift/pad methods available. So example, if you had numeric indices that you wanted to guarantee that index order is equal to insertion order, you'd create an array using $my_array = array_pad([], 20, null); If it's an array that you didn't create, the ksort(...) method will order the array for you as expected. Edit: after reading through the documentation on array_pad, it looks like it will even correct key order in existing arrays. https://www.php.net/manual/en/function.array-pad.php#87735 https://www.php.net/manual/en/function.array-pad.php#87735