$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.
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+...
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, threeYou 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.
Ideally the maintainers of PHP would rename it and deprecate the use of `array()` over a long period of time.
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.
$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.
$id = 12835151;
$arr[$id] = Get_thing_with_id( $id );Obviously, I'd agree that a map/associative array/dict/etc is a better choice here; I'm just annoyed about the name.
array_values($arr)[2];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
No, it didnt. What PHP calls "array" is actually an Ordered Map, as you alluded to:
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
The issue, if one exists, is that PHP doesnt have a sequence type:
https://yaml.org/type/seq
but one is provided as a package:
https://php.net/class.ds-sequence
https://github.com/php-ds/extension