note 52775 deleted from function.end by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Jun 2011 23:03:34 +0000 |
| Subject: | note 52775 deleted from function.end by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180585@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Fix for unknown writer of 29-aug-2002.
If you need to get a reference to the first or last element of an array, use these functions:
function &first(&$array) {
if (!is_array($array))
return null;
if (!count($array))
return false; // like reset()
reset($array);
return $array[key($array)];
}
function &last(&$array) {
if (!is_array($array))
return null;
if (!count($array))
return false; // like end()
end($array);
return $array[key($array)];
}
Example (watch out - use as &last() and &first()):
$a = array( 0 => 'less', 10 => 'ten', 20 => ' more');
if ($first = & first($a)) // not false or null
$first = 'nothing';
if ($last = & last($a)) // not false or null
$last = 'double';
Now $a is array( 0 => 'nothing', 10 => 'ten', 20 => '
double')