<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.in-array.php',
    1 => 'in_array',
    2 => 'Checks if a value exists in an array',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'prev' => 
  array (
    0 => 'function.extract.php',
    1 => 'extract',
  ),
  'next' => 
  array (
    0 => 'function.key.php',
    1 => 'key',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/in-array.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.in-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">in_array</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">in_array</span> &mdash; <span class="dc-title">Checks if a value exists in an array</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.in-array-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>in_array</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$haystack</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$strict</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Searches for <code class="parameter">needle</code> in <code class="parameter">haystack</code> using loose comparison
   unless <code class="parameter">strict</code> is set.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.in-array-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">needle</code></dt>
     <dd>
      <p class="para">
       The searched value.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        If <code class="parameter">needle</code> is a string, the comparison is done
        in a case-sensitive manner.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">haystack</code></dt>
     <dd>
      <p class="para">
       The array.
      </p>
     </dd>
    
    
     <dt><code class="parameter">strict</code></dt>
     <dd>
      <p class="para">
       If the third parameter <code class="parameter">strict</code> is set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
       then the <span class="function"><strong>in_array()</strong></span> function will also check the
       <a href="language.types.php" class="link">types</a> of the
       <code class="parameter">needle</code> in the <code class="parameter">haystack</code>.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="simpara">
        Prior to PHP 8.0.0, a non-numeric <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
        <code class="parameter">needle</code> would loosely match a
        <code class="parameter">haystack</code> value of <code class="literal">0</code>, and
        vice versa. As of PHP 8.0.0, the number is converted to a
        <span class="type"><a href="language.types.string.php" class="type string">string</a></span> and the two are compared as strings, so only a
        numeric <span class="type"><a href="language.types.string.php" class="type string">string</a></span> of the same value still matches.
       </p>
       <p class="simpara">
        Non-strict mode nonetheless uses
        <a href="types.comparisons.php#types.comparisions-loose" class="link">loose comparison</a>, so
        values of different types can still compare as equal: <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> matches
        any truthy <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, and <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> matches both
        <code class="literal">&quot;&quot;</code> and <code class="literal">&quot;0&quot;</code>. Unless the types of
        all values involved are known with certainty, always pass
        <code class="parameter">strict</code> to force an identity comparison.
       </p>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.in-array-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if <code class="parameter">needle</code> is found in the array,
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> otherwise.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.in-array-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>in_array()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$os </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"Mac"</span><span style="color: #007700">, </span><span style="color: #DD0000">"NT"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Irix"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Linux"</span><span style="color: #007700">);
if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"Irix"</span><span style="color: #007700">, </span><span style="color: #0000BB">$os</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"Got Irix"</span><span style="color: #007700">;
}
if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"mac"</span><span style="color: #007700">, </span><span style="color: #0000BB">$os</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"Got mac"</span><span style="color: #007700">;
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>
     The second condition fails because <span class="function"><strong>in_array()</strong></span>
     is case-sensitive, so the program above will display:
    </p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Got Irix</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>in_array()</strong></span> with strict example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$a </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'1.10'</span><span style="color: #007700">, </span><span style="color: #0000BB">12.4</span><span style="color: #007700">, </span><span style="color: #0000BB">1.13</span><span style="color: #007700">);

if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'12.4'</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"'12.4' found with strict check\n"</span><span style="color: #007700">;
}

if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">1.13</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"1.13 found with strict check\n"</span><span style="color: #007700">;
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">1.13 found with strict check</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Example #3 <span class="function"><strong>in_array()</strong></span> with an array as needle</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$a </span><span style="color: #007700">= array(array(</span><span style="color: #DD0000">'p'</span><span style="color: #007700">, </span><span style="color: #DD0000">'h'</span><span style="color: #007700">), array(</span><span style="color: #DD0000">'p'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">), </span><span style="color: #DD0000">'o'</span><span style="color: #007700">);

if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'p'</span><span style="color: #007700">, </span><span style="color: #DD0000">'h'</span><span style="color: #007700">), </span><span style="color: #0000BB">$a</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"'ph' was found\n"</span><span style="color: #007700">;
}

if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'f'</span><span style="color: #007700">, </span><span style="color: #DD0000">'i'</span><span style="color: #007700">), </span><span style="color: #0000BB">$a</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"'fi' was found\n"</span><span style="color: #007700">;
}

if (</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'o'</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">)) {
    echo </span><span style="color: #DD0000">"'o' was found\n"</span><span style="color: #007700">;
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">&#039;ph&#039; was found
&#039;o&#039; was found</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.in-array-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.array-search.php" class="function" rel="rdfs-seeAlso">array_search()</a> - Searches the array for a given value and returns the first corresponding key if successful</span></li>
    <li><span class="function"><a href="function.isset.php" class="function" rel="rdfs-seeAlso">isset()</a> - Determine if a variable is declared and is different than null</span></li>
    <li><span class="function"><a href="function.array-key-exists.php" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - Checks if the given key or index exists in the array</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>