note 24803 modified in function.end by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Jun 2011 23:04:07 +0000 |
| Subject: | note 24803 modified in function.end by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180587@lists.php.net to get a copy of this message | ||
When adding an element to an array, it may be interesting to know with which key it was added. Just
adding an element does not change the current position in the array, so calling key() won't
return the correct key value; you must first position at end() of the array:
<?php
function array_add(&$array, $value) {
$array[] = $value; // add an element
end($array); // important!
return key($array);
}
?>
--was--
When adding an element to an array, it may be interesting to know with which key it was added. Just
adding an element does not change the current position in the array, so calling key() won't
return the correct key value; you must first position at end() of the array:
function array_add(&$array, $value) {
$array[] = $value; // add an element
end($array); // important!
return key($array);
}
http://php.net/manual/en/function.end.php