0

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 as the value of an array - is there a reason this is not working, and can anyone suggest another way of doing this?

hakre
  • 193,403
  • 52
  • 435
  • 836
Darren
  • 3,049
  • 5
  • 21
  • 21
  • Something like in http://stackoverflow.com/questions/4330402/find-position-first-occurence-of-an-array-of-strings-in-a-string ? – KingCrunch Dec 02 '10 at 00:38
  • It's mixed because you can search for objects, strings, ints, bools, etc in an array - but it still only searches for a single needle. Use array_intersect() as explained below. – Cal Dec 02 '10 at 00:39

3 Answers3

3

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.

davethegr8
  • 11,323
  • 5
  • 36
  • 61
1

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.

alex
  • 479,566
  • 201
  • 878
  • 984
  • I believe it can take an `array` too, but the important part is it won't be an array of needles, it will be looking for an array that equals the one you passed. – Nicole Dec 02 '10 at 00:45
  • @Renesis My list of multiple types was just an example. I didn't actually go read the documentation :P – alex Dec 02 '10 at 00:49
  • doc for a particular function will explain whats allowed, but "mixed" doesn't represent any precise set of types – jon_darkstar Dec 02 '10 at 00:49
  • @jon_darkstar I know, it was meant to be an example only. – alex Dec 02 '10 at 00:51
0

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

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37