<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Checks if a value exists in an array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.extract.html">extract</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.key-exists.html">key_exists</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.in-array" class="refentry"> <div class="refnamediv"> <h1 class="refname">in_array</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">in_array</span> — <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="type">bool</span> <span class="methodname"><strong>in_array</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$haystack</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$strict</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> ] )</div> <p class="para rdfs-comment"> Searches <em><code class="parameter">haystack</code></em> for <em><code class="parameter">needle</code></em> using loose comparison unless <em><code class="parameter">strict</code></em> is set. </p> </div> <div class="refsect1 parameters" id="refsect1-function.in-array-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">needle</code></em></span> <dd> <p class="para"> The searched value. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If <em><code class="parameter">needle</code></em> is a string, the comparison is done in a case-sensitive manner. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">haystack</code></em></span> <dd> <p class="para"> The array. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">strict</code></em></span> <dd> <p class="para"> If the third parameter <em><code class="parameter">strict</code></em> is set to <strong><code>TRUE</code></strong> then the <span class="function"><strong>in_array()</strong></span> function will also check the <a href="language.types.html" class="link">types</a> of the <em><code class="parameter">needle</code></em> in the <em><code class="parameter">haystack</code></em>. </p> </dd> </dt> </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>TRUE</code></strong> if <em><code class="parameter">needle</code></em> is found in the array, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 changelog" id="refsect1-function.in-array-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.2.0</td> <td> <em><code class="parameter">needle</code></em> may now be an array. </td> </tr> </tbody> </table> </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-5013"> <p><strong>Example #1 <span class="function"><strong>in_array()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$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">);<br />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">)) {<br /> echo </span><span style="color: #DD0000">"Got Irix"</span><span style="color: #007700">;<br />}<br />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">)) {<br /> echo </span><span style="color: #DD0000">"Got mac"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></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="cdata"><pre> Got Irix </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5014"> <p><strong>Example #2 <span class="function"><strong>in_array()</strong></span> with strict example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$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">);<br /><br />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">)) {<br /> echo </span><span style="color: #DD0000">"'12.4' found with strict check\n"</span><span style="color: #007700">;<br />}<br /><br />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">)) {<br /> echo </span><span style="color: #DD0000">"1.13 found with strict check\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1.13 found with strict check </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5015"> <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="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$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">);<br /><br />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">)) {<br /> echo </span><span style="color: #DD0000">"'ph' was found\n"</span><span style="color: #007700">;<br />}<br /><br />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">)) {<br /> echo </span><span style="color: #DD0000">"'fi' was found\n"</span><span style="color: #007700">;<br />}<br /><br />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">)) {<br /> echo </span><span style="color: #DD0000">"'o' was found\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 'ph' was found 'o' 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 class="member"> <span class="function"><a href="function.array-search.html" class="function" rel="rdfs-seeAlso">array_search()</a> - Searches the array for a given value and returns the corresponding key if successful</span></li> <li class="member"> <span class="function"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - Determine if a variable is set and is not NULL</span></li> <li class="member"> <span class="function"><a href="function.array-key-exists.html" 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><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.extract.html">extract</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.key-exists.html">key_exists</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>