Wednesday, September 21, 2011

PHP: Get first key in Associative Array

If you want to get the very first key in associative array then use Reset and key method to get the key. Reset brings the pointer on first index and key returns the key name. Code snippet given below:

$shapes = array("Box" =>"Square","Moon" => "Circle");
//Set the internal pointer of an array to its first element
//get the first key that is "Box"
$keyname = key($shapes);