开发者

Can't search an array in PHP in_array for the presence of multiple needles

When giving PHP in_array an array of values I want to search for, it does not work. Although the documentation specifies that you can give a mixed needle.

So basically I want to test for the presence of multiple terms a开发者_JS百科s the value of an array - is there a reason this is not working, and can anyone suggest another way of doing this?


For something like searching an array $haystack for multiple $needles that are all strings or numbers, you can use array_intersect($array1, $array2, ...)

http://php.net/manual/en/function.array-intersect.php

It should work for mixed types also.


When the documentation says mixed it means it can accept multiple types, e.g. int, float, string. Though the docs should say exactly which types are valid.


mixed doesn't mean more than one needle, it refers to types. It's documentation talk for more than one possible type.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜