<!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>Searches the array for a given value and returns the corresponding key if successful</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-reverse.html">array_reverse</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-shift.html">array_shift</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.array-search" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_search</h1> <p class="verinfo">(PHP 4 >= 4.0.5, PHP 5)</p><p class="refpurpose"><span class="refname">array_search</span> — <span class="dc-title">Searches the array for a given value and returns the corresponding key if successful</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-search-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>array_search</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"> = false</span></span> ] )</div> <p class="para rdfs-comment"> Searches <em><code class="parameter">haystack</code></em> for <em><code class="parameter">needle</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-search-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>array_search()</strong></span> function will search for <em class="emphasis">identical</em> elements in the <em><code class="parameter">haystack</code></em>. This means it 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>, and objects must be the same instance. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-search-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the key for <em><code class="parameter">needle</code></em> if it is found in the array, <strong><code>FALSE</code></strong> otherwise. </p> <p class="para"> If <em><code class="parameter">needle</code></em> is found in <em><code class="parameter">haystack</code></em> more than once, the first matching key is returned. To return the keys for all matching values, use <span class="function"><a href="function.array-keys.html" class="function">array_keys()</a></span> with the optional <em><code class="parameter">search_value</code></em> parameter instead. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 changelog" id="refsect1-function.array-search-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>5.3.0</td> <td> As with all internal PHP functions as of 5.3.0, <span class="function"><strong>array_search()</strong></span> returns <strong><code>NULL</code></strong> if invalid parameters are passed to it. </td> </tr> <tr> <td>4.2.0</td> <td> Prior to PHP 4.2.0, <span class="function"><strong>array_search()</strong></span> returns <strong><code>NULL</code></strong> on failure instead of <strong><code>FALSE</code></strong>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.array-search-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4980"> <p><strong>Example #1 <span class="function"><strong>array_search()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">0 </span><span style="color: #007700">=> </span><span style="color: #DD0000">'blue'</span><span style="color: #007700">, </span><span style="color: #0000BB">1 </span><span style="color: #007700">=> </span><span style="color: #DD0000">'red'</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=> </span><span style="color: #DD0000">'green'</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">=> </span><span style="color: #DD0000">'red'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">, </span><span style="color: #0000BB">$array</span><span style="color: #007700">); </span><span style="color: #FF8000">// $key = 2;<br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">, </span><span style="color: #0000BB">$array</span><span style="color: #007700">); </span><span style="color: #FF8000">// $key = 1;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-search-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-keys.html" class="function" rel="rdfs-seeAlso">array_keys()</a> - Return all the keys or a subset of the keys of an array</span></li> <li class="member"> <span class="function"><a href="function.array-values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - Return all the values of an array</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> <li class="member"> <span class="function"><a href="function.in-array.html" class="function" rel="rdfs-seeAlso">in_array()</a> - Checks if a value exists in an 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.array-reverse.html">array_reverse</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-shift.html">array_shift</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>