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')