I have an array that looks like this:
$fruit = array('apple','orange','grape');
How can I find the index of a specific item, in the above array? (For example, the value ‘orange’)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try the array_search function.
From the first example in the manual:
A word of caution
When comparing the result, make sure to test explicitly for the value
falseusing the===operator.Because arrays in PHP are 0-based, if the element you’re searching for is the first element in the array, a value of 0 will be returned.
While 0 is a valid result, it’s also a falsy value, meaning the following will fail:
This is because the
==operator checks for equality (by type-juggling), while the===operator checks for identity.