By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

array

P: 1
How to print the keyname using array.is it possible?
Aug 1 '07 #1
Share this Question
Share on Google+
4 Replies


nathj
Expert 100+
P: 938
How to print the keyname using array.is it possible?
If you have associative array called $laArray:

[php]
<?

$laArray['peter'] = 23;
$laArray['sarah'] = 25;
$laArray['steve'] = 27;
$laArray['ann'] = 52;
$laArray['andy'] = 55;

foreach($laArray as $key => $value)
{
echo "Key: $key; Value: $value <br />";
}
?>
[/php]

I believe that does the trick.

Cheers
nathj
Aug 1 '07 #2

Motoma
Expert 2.5K+
P: 3,237
If you have associative array called $laArray:

[php]
<?

$laArray['peter'] = 23;
$laArray['sarah'] = 25;
$laArray['steve'] = 27;
$laArray['ann'] = 52;
$laArray['andy'] = 55;

foreach($laArray as $key => $value)
{
echo "Key: $key; Value: $value <br />";
}
?>
[/php]

I believe that does the trick.

Cheers
nathj
If that isn't good enough:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $laArray['peter'] = 23;
  3. $laArray['sarah'] = 25;
  4. $laArray['steve'] = 27;
  5. $laArray['ann'] = 52;
  6. $laArray['andy'] = 55;
  7.  
  8. foreach($laArray as $value)
  9. {
  10.     echo "Key: ".key($value)."; Value: $value <br />";
  11. }
  12.  
Aug 1 '07 #3

100+
P: 137
or you can do:
[PHP]
$pietje = array ("hello", "car", "airplane");

print_r ($pietje['hello']);

[/PHP]
Displays:
[HTML]hello[/HTML]

Gr Paul
Aug 1 '07 #4

kovik
Expert 100+
P: 1,044
or you can do:
[PHP]
$pietje = array ("hello", "car", "airplane");

print_r ($pietje['hello']);

[/PHP]
Displays:
[HTML]hello[/HTML]

Gr Paul
... No. No it wouldn't. :-/

Motoma's suggestion to use the key() function is likely the correct answer.
Aug 2 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.