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:
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
<php-array-key-index.patch.txt><array_key_index.phpt.txt>
--
John Bafford
[EMAIL PROTECTED]
http://www.dshadow.com/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php