开发者

PHP Array Find Object's Index

开发者 https://www.devze.com 2023-01-13 06:12 出处:网络
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 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


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);


$index = array_search('something', $myarray)


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

0

精彩评论

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