[PATCH] New functions: array_key_index(), array_first(), array_last()

php.internals

John Bafford

19 years ago
Hello, Attached is a patch and corresponding test file I would like to submit for inclusion in PHP. I would like this patch to be included in PHP 5.2.1, as it is entirely new code (and thus shouldn't cause regressions), but as RC2 was just released earlier today, I understand if it must wait until PHP 5.2.2. This patch implements three new functions, as described below. mixed array_key_index(array input, int index [, mixed value]) Return the array's index'th key (and optionally, value) mixed array_first(array input [, mixed value]) Return the array's first key (and optionally, value) This is equivalent to array_key_index($input, 0 [, $value]); mixed array_last(array input [, mixed value]) Return the array's last key (and optionally, value) This is equivalent to array_key_index($input, -1 [, $value]); array_first() is a non-destructive way to get the first key (and value, if requested) from an array. array_first() is intended to replace code similar to the following: foreach($arr as $key => $val) break; reset($arr); $key = key($arr); reset($arr); list($key, $val) = each($arr); $key = array_keys($arr); $key = $key[0]; array_last() is the obvious counterpart to array_first(), returning the last key (and value, if requested) from an array. array_first() and array_last() are implemented via array_key_index(), which returns a key based on its order in the array. The index parameter functions similarly to substr()'s start parameter (>= 0 start from the beginning of the array, <= -1 start from the end.) All three functions leave the original array unmodified. In the event of an error (invalid parameters, or attempting to seek past the end of the array), all three functions return NULL and leave $value unchanged. Please let me know if you have any comments. Thanks, -John

Andrei Zmievski

19 years ago
These functions seem to replace only a couple lines of code at best, especially array_first() and array_last(). I don't think the cost of keeping up the code/docs for the new functions warrants their inclusion. -Andrei On Jan 4, 2007, at 9:49 PM, John Bafford wrote:

Brian Moon

19 years ago
Andrei Zmievski wrote:
> These functions seem to replace only a couple lines of code at best, > especially array_first() and array_last(). I don't think the cost of > keeping up the code/docs for the new functions warrants their inclusion.
The same could be said for half the array functions. array_sum() is like 3 lines. IMO, the better question is, are these useful additions for people that work with a lot of arrays. IMO, they are good additions to a very useful set of array functions.
-- Brian Moon ------------- http://dealnews.com/ It's good to be cheap =)

Antony Dovgal

19 years ago
On 01/05/2007 08:49 AM, John Bafford wrote:
> Hello, > > Attached is a patch and corresponding test file I would like to > submit for inclusion in PHP. I would like this patch to be included > in PHP 5.2.1, as it is entirely new code (and thus shouldn't cause > regressions), but as RC2 was just released earlier today, I > understand if it must wait until PHP 5.2.2. > > This patch implements three new functions, as described below. > > mixed array_key_index(array input, int index [, mixed value]) > Return the array's index'th key (and optionally, value) > > mixed array_first(array input [, mixed value]) > Return the array's first key (and optionally, value) > This is equivalent to array_key_index($input, 0 [, $value]); > > mixed array_last(array input [, mixed value]) > Return the array's last key (and optionally, value) > This is equivalent to array_key_index($input, -1 [, $value]);
-1 Implementing these functions _in_ PHP can't take more than a minute.
-- Wbr, Antony Dovgal

Arnold Daniels

19 years ago
+1 Currently there is no way to get a key from an array without either moving the cursor or first getting all keys and go from there. While these function are easily written in PHP, the implementation feels more like a dirty workaround than a solid solution. I would personally use these functions and I think many others will as well, so they are worth being included. Arnold Antony Dovgal schreef: