Get Key by Value? (Array)

I have an array
lets say for this example:
      $chars[] = 'a';
      $chars[] = 'b';
      $chars[] = 'c';
      $chars[] = 'd';
      $chars[] = 'e';
      $chars[] = 'f';
      $chars[] = 'g';
      $chars[] = 'h';
      $chars[] = 'i';
      $chars[] = 'j';

echo getKeyByValue('d') ; // 3
echo getKeyByValue('a') ; // 0
echo getKeyByValue('j') ; // 9

How can I do the currently non-existant function getKeybyValue() ?
LVL 5
mnb93Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
$key = array_search('d', $chars);

Should return '3';

-r-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TomeeboyCommented:
function getKeyByValue ($array, $value) {
    $results = array_keys($array, $value);
    return $results[0];
}
TomeeboyCommented:
Ah, I basically just remade array_search()  :)

If you want multiple results returned, array_keys() will return an array of keys that had values matching what you searched for.  If you just want the first one, then array_search should work fine.
WaielEidCommented:
it's the same as tomeeboy said
but if you want one arguemnt you have to globlize the array

function getKeyByValue ($value) {
    global $chars;
    $results = array_keys($chars, $value);
    return $results[0];
}

which is also array seach can do: :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.