note 92704 added to function.count

From: Date: Tue, 04 Aug 2009 19:37:48 +0000
Subject: note 92704 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-159108@lists.php.net to get a copy of this message
The code snippet posted by legobuff at hotmail dot com on 02-Feb-2000 contains an error. This is the code that was posted: <?php // To find the index of the last element in the array: end($yourArray); $index = key($yourArray); ?> However, this will fail if the indices have been stored out of sequence. What you actually want is max(array_keys($yourArray)), as indicated below: <?php $yourArray = array(1=>'a', 7=>'b', 5=>'c'); print count($yourArray); // prints 3 end($yourArray); print key($yourArray); // prints 5 print max(array_keys($yourArray)); // prints 7 ?> ---- Server IP: 209.41.74.194 Probable Submitter: 67.165.168.203 ---- Manual Page -- http://www.php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/92704 Del: integrated -- https://master.php.net/note/delete/92704/integrated Del: useless -- https://master.php.net/note/delete/92704/useless Del: bad code -- https://master.php.net/note/delete/92704/bad+code Del: spam -- https://master.php.net/note/delete/92704/spam Del: non-english -- https://master.php.net/note/delete/92704/non-english Del: in docs -- https://master.php.net/note/delete/92704/in+docs Del: other reasons-- https://master.php.net/note/delete/92704 Reject -- https://master.php.net/note/reject/92704 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159108) next »