2) array_search function
array_search function is similar to in_array function takes same argument and behaves exactly like in_array function except for the value it returns. Unlike in_array function, array_search function return the key of the value when the search is successful (key of first matched element) and false when the search is unsuccessful. Since an element can have a numeric key 0 which converts to false when converted into a boolean value, make sure you use === comparison operator when determining that a search was successful or not.
If you wish to retrieve the keys of all matching elements instead of retreiving the key of just the first matching element, you can use array_keys function.
|