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

share|improve this question
feedback

3 Answers

up vote 4 down vote accepted

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);
share|improve this answer
2  
Thanks. These are all good answers but I'ma go with this one. – Christian Stewart Aug 28 '10 at 19:36
feedback

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

share|improve this answer
indices fwiw ;) ... and actually they're keys, since they don't necessarily have to be numbers. – Peter Ajtai Aug 28 '10 at 19:24
Thanks Peter =) – Headshota Aug 28 '10 at 19:25
1  
Thanks. These are all good answers but I'ma go with the most detailed one :D – Christian Stewart Aug 28 '10 at 19:36
feedback
$index = array_search('something', $myarray)
share|improve this answer
This is not in the PHP standard library – NullUserException Aug 28 '10 at 19:15
this really exists? in PHP? – Garis M Suero Aug 28 '10 at 19:16
1  
Actually wrong text was pasted. Thanks for correction. – NAVEED Aug 28 '10 at 19:17
2  
Thanks. These are all good answers but I'ma go with the most detailed one :D – Christian Stewart Aug 28 '10 at 19:36
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.