Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2092

php-manual-en-5.5.7-1.mga4.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>Removes duplicate values from an array</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-uintersect.html">array_uintersect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-unshift.html">array_unshift</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-unique" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_unique</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">array_unique</span> &mdash; <span class="dc-title">Removes duplicate values from an array</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.array-unique-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>array_unique</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flags</code><span class="initializer"> = SORT_STRING</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Takes an input <em><code class="parameter">array</code></em> and returns a new array
   without duplicate values.
  </p>
  <p class="para">
   Note that keys are preserved.  <span class="function"><strong>array_unique()</strong></span> sorts
   the values treated as string at first, then will keep the first key
   encountered for every value, and ignore all following keys. It does not
   mean that the key of the first related value from the unsorted
   <em><code class="parameter">array</code></em> will be kept.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Two elements are considered equal if and only if
    <em>(string) $elem1 === (string) $elem2</em>. In words:
    when the string representation is the same.
   </span>
   <span class="simpara">
    The first element will be used.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.array-unique-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">array</code></em></span>
     <dd>

      <p class="para">
       The input array.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">sort_flags</code></em></span>
     <dd>

      <p class="para">
       The optional second parameter <em><code class="parameter">sort_flags</code></em>
       may be used to modify the sorting behavior using these values:
      </p>
      <p class="para">
       Sorting type flags:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_REGULAR</code></strong> - compare items normally
         (don&#039;t change types)</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - compare items numerically</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_STRING</code></strong> - compare items as strings</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_LOCALE_STRING</code></strong> - compare items as
          strings, based on the current locale.
          
         </span>
        </li>
       </ul>
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-unique-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the filtered array.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-unique-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.2.10</td>
       <td>
        Changed the default value of <em><code class="parameter">sort_flags</code></em> back to
        <strong><code>SORT_STRING</code></strong>.
       </td>
      </tr>

      <tr>
       <td>5.2.9</td>
       <td>
        Added the optional <em><code class="parameter">sort_flags</code></em> defaulting
        to <strong><code>SORT_REGULAR</code></strong>. Prior to 5.2.9, this function used to sort
        the array with <strong><code>SORT_STRING</code></strong> internally.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-unique-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4993">
    <p><strong>Example #1  <span class="function"><strong>array_unique()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</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>
Array
(
    [a] =&gt; green
    [0] =&gt; red
    [1] =&gt; blue
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4994">
    <p><strong>Example #2  <span class="function"><strong>array_unique()</strong></span> and types</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"4"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"3"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</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>
array(2) {
  [0] =&gt; int(4)
  [2] =&gt; string(1) &quot;3&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.array-unique-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.array-count-values.html" class="function" rel="rdfs-seeAlso">array_count_values()</a> - Counts all the values of an array</span></li>
   </ul>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.array-unique-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Note that  <span class="function"><strong>array_unique()</strong></span> is not intended to 
    work on multi dimensional arrays.
   </span>
  </p></blockquote>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-uintersect.html">array_uintersect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-unshift.html">array_unshift</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>