smus Posted February 1, 2017 Share Posted February 1, 2017 How to get an array index by its value (key)? Is there a special function for that in PHP? For example, I have an array $a ["zero","one","two"] and a variable $b = "two", I know the value is in the array, but I don't know its index. Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2017 Share Posted February 1, 2017 You have to iterate through the array and compare each value to $b, when you find a match return the key. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 1, 2017 Share Posted February 1, 2017 You can also use this: http://php.net/manual/en/function.array-search.php 1 Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2017 Share Posted February 1, 2017 Oh, right. PHP has it built in. I was on my phone so I wasn't checking the reference. Link to comment Share on other sites More sharing options...
smus Posted February 2, 2017 Author Share Posted February 2, 2017 array_search function! Yes, thanks very much, guys! Very helpful! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now