Home > Web Development > PHP – 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);
Advertisements
Categories: Web Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: