<!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 >= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_unique</span> — <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 <code class="parameter">array</code> and returns a new array without duplicate values. </p> <p class="para"> Note that keys are preserved. If multiple elements compare equal under the given <code class="parameter">sort_flags</code>, then the key and value of the first equal element will be retained. </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> i.e. when the string representation is the same, 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> <code class="parameter">array</code></dt> <dd> <p class="para"> The input array. </p> </dd> <dt> <code class="parameter">sort_flags</code></dt> <dd> <p class="para"> The optional second parameter <code class="parameter">sort_flags</code> 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'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> </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>7.2.0</td> <td> If <code class="parameter">sort_flags</code> is <strong><code>SORT_STRING</code></strong>, formerly <code class="parameter">array</code> has been copied and non-unique elements have been removed (without packing the array afterwards), but now a new array is built by adding the unique elements. This can result in different numeric indexes. </td> </tr> <tr> <td>5.2.10</td> <td> Changed the default value of <code class="parameter">sort_flags</code> back to <strong><code>SORT_STRING</code></strong>. </td> </tr> <tr> <td>5.2.9</td> <td> Added the optional <code class="parameter">sort_flags</code> 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-6209"> <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"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </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">?></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] => green [0] => red [1] => blue ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6210"> <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"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #DD0000">"4"</span><span style="color: #007700">, </span><span style="color: #DD0000">"3"</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"3"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </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">?></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] => int(4) [2] => string(1) "3" } </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>