Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 2606

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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 first 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 &gt;= 4.0.5, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_search</span> &mdash; <span class="dc-title">Searches the array for a given value and returns the first 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"> = <strong><code>FALSE</code></strong></span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Searches <code class="parameter">haystack</code> for <code class="parameter">needle</code>.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-search-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>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
       <code class="parameter">haystack</code>. This means it will also perform a
       <a href="language.types.html" class="link">strict type comparison</a> of the
       <code class="parameter">needle</code> in the <code class="parameter">haystack</code>,
       and objects must be the same instance.
      </p>
     </dd>

    
   </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 <code class="parameter">needle</code> if it is found in the
   array, <strong><code>FALSE</code></strong> otherwise.
  </p>
  <p class="para">
   If <code class="parameter">needle</code> is found in <code class="parameter">haystack</code>
   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
   <code class="parameter">search_value</code> 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>

     </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-6194">
    <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">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'blue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;2;<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;1;<br /></span><span style="color: #0000BB">?&gt;</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>