Web DevelopmentPHP – Getting Array Index Values

PHP – Getting Array Index Values

PHP has many array functions for arrays
array_keys – returns all the keys of an array

Lets say you have an array like

$a = array(1=>'q',2=>'w',3=>'h');
foreach($a as $key=>$value)  echo $key;

This above prints out all the key values. So if you want to know the key of w, you would say

foreach($a as $key=>$value) {    
    if($value == 'w') echo $key;

To get the first “key” in an associative array

 reset($myArray); // make sure array pointer is at first element
$firstKey = key($myArray);

Categories: Web Development


No Comments Yet. Be the first?

Post a comment

Your email address will not be published. Required fields are marked *