note 53054 deleted from function.key by googleguy

From: Date: Tue, 22 Oct 2013 06:42:47 +0000
Subject: note 53054 deleted from function.key by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196607@lists.php.net to get a copy of this message
Note Submitter: Danniel@t h e - police (d'0't) com ---- To get the key name by position from Array: <?php $myArray['name1']=3; $myArray['name2']=2; $myArray['name3']=1; echo($myArray[1]); /* return NULL */ /* isset($myArray[1]) return false; */ /* is_null($myArray[1]) return true; */ function KeyName($myArray,$pos) { // $pos--; /* uncomment the above line if you */ /* prefer position to start from 1 */ if ( ($pos < 0) || ( $pos >= count($myArray) ) ) return "NULL"; // set this any way you like reset($myArray); for($i = 0;$i < $pos; $i++) next($myArray); return key($myArray); } echo KeyName($myArray,1); // result: name2 echo KeyName($myArray,2); // result: name3 echo KeyName($myArray,3); // result: "NULL" ?> You can get any existing "key name" from array that is located at position $pos gived as second parameter. If no element is defined at given position the function will return the string "NULL".

« previous php.notes (#196607) next »