views:

37

answers:

3

Hello,

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  A: 

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);
NullUserException
Thanks. These are all good answers but I'ma go with this one.
Christian Stewart
+1  A: 
$index = array_search('something', $myarray)
NAVEED
This is not in the PHP standard library
NullUserException
this really exists? in PHP?
Garis Suero
Actually wrong text was pasted. Thanks for correction.
NAVEED
Thanks. These are all good answers but I'ma go with the most detailed one :D
Christian Stewart
+1  A: 

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

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