note 100828 added to function.key

From: Date: Wed, 10 Nov 2010 03:04:48 +0000
Subject: note 100828 added to function.key
Groups: php.notes 
Request: Send a blank email to php-notes+get-173892@lists.php.net to get a copy of this message
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 ---- Server IP: 208.69.120.55 Probable Submitter: 216.99.199.215 ---- Manual Page -- http://www.php.net/manual/en/function.key.php Edit -- https://master.php.net/note/edit/100828 Del: integrated -- https://master.php.net/note/delete/100828/integrated Del: useless -- https://master.php.net/note/delete/100828/useless Del: bad code -- https://master.php.net/note/delete/100828/bad+code Del: spam -- https://master.php.net/note/delete/100828/spam Del: non-english -- https://master.php.net/note/delete/100828/non-english Del: in docs -- https://master.php.net/note/delete/100828/in+docs Del: other reasons-- https://master.php.net/note/delete/100828 Reject -- https://master.php.net/note/reject/100828 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173892) next »