3

How would I figure out where a specific item is in an array? For instance I have an array like this:

("itemone", "someitem", "fortay", "soup")

How would I get the index of "someitem"

Thanks, Christian Stewart

3 Answers 3

5

Use array_search()

array_search — Searches the array for a given value and returns the corresponding key if successful

mixed array_search ( mixed $needle , array $haystack [, bool $strict ] )

Example:

$key = array_search('someitem', $array);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. These are all good answers but I'ma go with this one.
array_search seems very slow.
2
$index = array_search('something', $myarray)

2 Comments

This is not in the PHP standard library
Thanks. These are all good answers but I'ma go with the most detailed one :D
1

You can also use array_keys($array,$search); to return multiple keys (indices) for given value

2 Comments

indices fwiw ;) ... and actually they're keys, since they don't necessarily have to be numbers.
Thanks. These are all good answers but I'ma go with the most detailed one :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.