AleGore
AleGore

Reputation: 303

PHP: How to find out the position of a value in an array?

How to find out that foo is at the position of 2?: array('boo', 'moo', 'foo');

Upvotes: 2

Views: 299

Answers (3)

Decent Dabbler
Decent Dabbler

Reputation: 22783

If you want to find the keys for all occurences of 'foo' (if you know there will be duplicates) then use:

$result = array_keys( $yourArray, 'foo' );

This will return an array with all corresponding keys. You see, array_search will only return the key of the first occurence. Be aware of this.

Upvotes: 2

Pavunkumar
Pavunkumar

Reputation: 5335

print_r ( array_search ( 'value',$array_from ) );

Upvotes: 4

Tyler Carter
Tyler Carter

Reputation: 61567

$key = array_search("foo", $array);

Upvotes: 10

Related Questions