Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Associative Array Question

Status
Not open for further replies.

JerryH

Programmer
Jul 8, 2000
15
0
0
US
Here is my array:

$option = array (
'fruit' => array ('banana','apple'),
'vegetable' => array ('cucumber','lettuce'),
'nut' => array ('almond','peanut')
);

All I want to do is print the second key. How can I write this?

<?php print $option[key[1]] ?>

I want the above to print out 'vegetable'.
 
You want to use the array_keys() function:
Code:
$option = array (
'fruit' => array ('banana','apple'),
'vegetable' => array ('cucumber','lettuce'),
'nut' => array ('almond','peanut')
);

$option_keys = array_keys($option);
echo $option_keys[1];
Remember that arrays (non-associative) start with the index 0.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top