obtaining array key from a value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

obtaining array key from a value

Post by davidklonski »

Hello

assuming I have the following array definition:

Code: Select all

<?php
$a = Array('a' => 'one',  'b' => 'two',  'c' => 'three',  'd' => 'four');
?>
I have a variable holding one of the array's values (for example, 'four'). Is there a php function which can give me the key that matches that value in the array? For example, in this case the key would be 'd'

thanks in advance
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

$the_key = array_search($your_variable, $a);
Post Reply