Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last()
| From: | Woortmann, Enno | Date: | Thu, 28 Jun 2018 12:48:42 +0000 |
| Subject: | Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last() | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102512@lists.php.net to get a copy of this message | ||
Hi David,
thanks for your brainstorming suggestions. My opinion on the options:
1. Keys being stored as reference: $valueOutput = array_first(array $array, ?string &$keyOutput = null); In my opinion using references to return additional values from a function is quiet hacky. I think returning a single value leads to significantly clearer code and thus supports the developer.
2. Function will returns value or key, by option (similar to array_filter()): array_first(array $array, int $options = ARRAY_VALUE); array_first(array $array, int $options = ARRAY_KEY); This violates the single responsibility principle and consequently isn't a good move
3. F unction will returns value or key, by boolean argument: array_first(array $array, bool $returnKeyInsteadOfValue = false); array_first(array $array, bool $returnKeyInsteadOfValue = true); Compare 2.
4. Specific method to return key instead of value: array_first(array $array); array_first_key(array $array); This is the RFC just with slightly different function names. I think using array_key_first and array_value_first (and the corresponding functions for the last array element) provides a consistent naming.
5. Consider that keys could be obtained via array_keys(), so array_first() should just care about values: array_first(array_keys($array)); This solution requires a copy of the array (keys) which extends the time complexity and the memory usage of the task.Regards, Enno