note 24804 modified in function.end by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Jun 2011 23:03:57 +0000 |
| Subject: | note 24804 modified in function.end by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180586@lists.php.net to get a copy of this message | ||
If you need to get a reference on the first or last element of an array, use these functions because
reset() and end() only return you a copy that you cannot dereference directly:
<?php
function first(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
reset($array);
return &$array[key($array)];
}
function last(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
end($array);
return &$array[key($array)];
}
?>
--was--
If you need to get a reference on the first or last element of an array, use these functions because
reset() and end() only return you a copy that you cannot dereference directly:
function first(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
reset($array);
return &$array[key($array)];
}
function last(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
end($array);
return &$array[key($array)];
}
http://php.net/manual/en/function.end.php