note 29950 modified in function.current by danbrown
| From: | danbrown@php.net | Date: | Fri, 10 Sep 2010 14:55:12 +0000 |
| Subject: | note 29950 modified in function.current by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172446@lists.php.net to get a copy of this message | ||
The docs do not specify this, but adding to the array using the brackets syntax:
<?php $my_array[] = $new_value; ?>
will not advance the internal pointer of the array. therefore, you cannot use current() to get the
last value added or key() to get the key of the most recently added element.
You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the
notes on end() ), then
<?php
$last_key = key($my_array); // will return the key
$last_value = current($my_array); // will return the value
?>
If you have no need in the key, $last_value = end($my_array) will also do the job.
- Sergey.
--was--
The docs do not specify this, but adding to the array using the brackets syntax:
$my_array[] = $new_value;
will not advance the internal pointer of the array. therefore, you cannot use current() to get the
last value added or key() to get the key of the most recently added element.
You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the
notes on end() ), then
$last_key = key($my_array); // will return the key
$last_value = current($my_array); // will return the value
If you have no need in the key, $last_value = end($my_array) will also do the job.
- Sergey.
http://php.net/manual/en/function.current.php