42326

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

Question:

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 <em>mixed</em> 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?

Answer1:

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

Answer2:

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

<a href="http://php.net/manual/en/function.array-intersect.php" rel="nofollow">http://php.net/manual/en/function.array-intersect.php</a>

It should work for mixed types also.

Answer3:

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.

Recommend

  • Parent Child SQL Recursion
  • Mongoid same embedded documents types for different fields
  • Client JS + Django Rest Framework
  • Need advice in designing tables in SQL-Server
  • How to make Javascript generated checkboxes persist?
  • web shop (shopping cart) on google app engine
  • PHP & Parsing a JSON response
  • Class implementation in a header file == bad style? [duplicate]
  • What does a hyphen at end of a term mean
  • Obtain actual browser URL in PHP
  • How does inheritance and polymorphism work in this situation?
  • Ruby and class variables in inherit class
  • Can XOR be expressed using SKI combinators?
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Where these are stored?
  • Most efficient way to move table rows from one table to another
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • What does “t” refer to in this SQL?
  • Trying to get the char code of ENTER key
  • Cast between interfaces whose interface signatures are same
  • Using Sax parsing to edit and write XML in VB6
  • Debug.DrawLine not showing in the GameView
  • Refering to the class itself from within a class mehod in Objective C
  • Yii2: Config params vs. const/define
  • C++ Partial template specialization - design simplification
  • How to delay loading a property with linq to sql external mapping?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • Bug in WPF DataGrid
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • Matrix multiplication with MKL
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Converting MP3 duration time
  • java string with new operator and a literal