Given an associative array like the following,
$field_defaults = array( 'id' => 0, 'name' => 'new item', 'desc' => '', 'parent_id' => 0, );
can I rely on array_keys()
returning the keys in the order they were specified? Or, more precisely, since arrays in PHP seem to have a stable order, as per this answer, are the keys returned by array_keys()
in the same order as they appear in the input array? The manual page doesn't give any hints.
When I try this, they seem to respect the original order, but I would like to be able to rely on that behaviour.
The array_keys() function returns all the keys of an array. It returns an array of all the keys in array.
The array_values() function returns the array containing all the values of an array. The returned array will have the numeric keys, starting at 0 and increasing by 1. The PHP array_keys() function creates another array that stores all the values and by default assigns numerical keys to the values.
The array_keys() function is used to get all the keys or a subset of the keys of an array. Version: (PHP 4 and above) Syntax: array_keys(input_array, search_key_value, strict) Note: If the optional search_key_value is specified, then only the keys for that value are returned.
TL;DR: Theoretically you can't count on it; for practical purposes IMO you can.
Since the docs do not guarantee the ordering then technically the correct answer would be "no, you can't count on that".
That's because theoretically the developers could have chosen to reserve themselves the option of changing the implementation at a future date so that it does not honor the existing order any more (perhaps to improve performance, or to gain some other benefit).
Now as a practical matter, we know that the current implementation honors the ordering -- PHP arrays are ordered containers (there is a linked list of values among other things) -- and this is something you wouldn't ever expect to change.
If it did, the change would hint to a corresponding significant change in the internal implementation of arrays and that would in turn be likely to break lots of other code too. I don't see it happening any time soon.
If you are concerned, you could always pick one as the correct order and then reimplement the other function based on that. And if you care about consistency between the two calls, you probably are going to call both array_keys
and array_values
at the same time. So why not do both simultaneously? E.g., let's assume that the order of array_keys()
is "correct". Then do:
function arrayKV($arr) { $keys = array_keys($arr); $values = array(); foreach($keys as $key) { $values[] = $arr[$key]; } return array('keys' => $keys, 'values' => $values); }
That way, you know that they are in the same order. Alternatively, you could provide the keys to use as the order:
function arrayValuesStable($arr, $keys) { $values = array(); foreach($keys as $key) { $values[] = $arr[$key]; } return $values; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With