<HTML ><HEAD ><TITLE >array_unique</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_sum" HREF="function.array-sum.html"><LINK REL="NEXT" TITLE="array_unshift" HREF="function.array-unshift.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-sum.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-unshift.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-unique" ></A >array_unique</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8213" ></A ><P > (PHP 4 >= 4.0.1)</P >array_unique -- Removes duplicate values from an array</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8216" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_unique</B > ( array array)<BR ></BR ><P > <B CLASS="function" >array_unique()</B > takes input <TT CLASS="parameter" ><I >array</I ></TT > and returns a new array without duplicate values. </P ><P > Note that keys are preserved. <B CLASS="function" >array_unique()</B > 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 <TT CLASS="parameter" ><I >array</I ></TT > will be kept. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Two elements are considered equal if and only if <TT CLASS="literal" >(string) $elem1 === (string) $elem2</TT >. In words: when the string representation is the same. </P ><P > The first element will be used. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > This was broken in PHP 4.0.4! </P ></TD ></TR ></TABLE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8237" ></A ><P ><B >Example 1. <B CLASS="function" >array_unique()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$input = array ("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique ($input); print_r($result);</PRE ></TD ></TR ></TABLE ><P > This will output: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [b] => green [1] => blue [2] => red )</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8244" ></A ><P ><B >Example 2. <B CLASS="function" >array_unique()</B > and types</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$input = array (4,"4","3",4,3,"3"); $result = array_unique ($input); var_dump($result);</PRE ></TD ></TR ></TABLE ><P > The printout of the program above will be (PHP 4.0.6): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >array(2) { [3]=> int(4) [4]=> int(3) }</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.array-sum.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-unshift.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_sum</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_unshift</TD ></TR ></TABLE ></DIV ></BODY ></HTML >