note 52775 added to function.end
| From: | php-general at lists dot php dot net | Date: | Thu, 12 May 2005 08:21:26 +0000 |
| Subject: | note 52775 added to function.end | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89304@lists.php.net to get a copy of this message | ||
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')
----
Manual Page -- http://www.php.net/manual/en/function.end.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52775
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52775&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52775&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52775&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52775&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52775&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52775&report=yes
Search -- http://master.php.net/manage/user-notes.php