This is the method I use to find the key of a requested item in an array,
$items = array
(
'0' => array
(
'mnu_id' => '1',
'pg_url' => 'home'
),
'1' => array
(
'mnu_id' => '5',
'pg_url' => 'about'
),
'2' => array
(
'mnu_id' => '6',
'pg_url' => 'venues'
)
);
So if I request the value of ‘venues’,
while ($current = current($items))
{
if ($current['pg_url'] == 'venues') {
$current_key = key($items);
}
next($items);
}
echo $current_key;
I get the key which is 2.
I don’t really like this method as it is a bit lengthy and it confuses me when using while() to loop the array. I don’t understand why I have to use next() in the code too!
I wonder if there are any better method than this to get the key?
Maybe this is a little easier to understand:
I don’t know, what exactly you are trying to do, but it seems like you want to retrieve information from the array depending on the pg_url… if so, maybe it would be nicer to set the pg_url as key:
then you could simply check for