开发者

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

开发者 https://www.devze.com 2022-12-22 01:02 出处:网络
How to find out that foo is at the p开发者_运维百科osition of 2?: array(\'boo\', \'moo\', \'foo\');$key = array_search(\"foo\", $array);

How to find out that foo is at the p开发者_运维百科osition of 2?: array('boo', 'moo', 'foo');


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号