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
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
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 tosubstr()
'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 returnNULL
and leave $value
unchanged.Please let me know if you have any comments.
Thanks,
-John
<php-array-key-index.patch.txt><array_key_index.phpt.txt>
John Bafford
dshadow@zort.net
http://www.dshadow.com/
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 =)
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
+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:
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.