note 100828 rejected from function.key by thiago
| From: | thiago@php.net | Date: | Wed, 10 Nov 2010 12:14:01 +0000 |
| Subject: | note 100828 rejected from function.key by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173897@lists.php.net to get a copy of this message | ||
Note Submitter: chris at threeprong dot com
----
The internal array pointer doesn't seem to move unless you tell it to, which appears to be
consistent with the documentation.
Wouldn't be handy if empty brackets or array_push would had an easy method to locate the last
automatically assigned key? Seems like the reasonable option here would be to to call end() followed
by key(), but that might not always be safe with arrays having mixed keys.
The following code:
<?php
$ar[] = "one"; // append to some array
$i = key($ar);
echo "Internal pointer aimed at element $i holding $ar[$i]\n";
$ar[] = "two";
$i = key($ar);
echo "Internal pointer aimed at element $i holding $ar[$i]\n";
$ar[] = "three";
end($ar); // coax pointer to the end
$i = key($ar);
echo "Internal pointer aimed at element $i holding $ar[$i]\n";
array_push($ar, "four");
$i = key($ar);
echo "Internal pointer aimed at element $i holding $ar[$i]\n";
?>
produces the output:
Internal pointer aimed at element 0 holding one
Internal pointer aimed at element 0 holding one
Internal pointer aimed at element 2 holding three
Internal pointer aimed at element 2 holding three