Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 4986

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>usort</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Arrays"
HREF="ref.array.html"><LINK
REL="PREVIOUS"
TITLE="uksort"
HREF="function.uksort.html"><LINK
REL="NEXT"
TITLE="Aspell [obsoleta]"
HREF="ref.aspell.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.uksort.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="ref.aspell.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.usort"
></A
>usort</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN13219"
></A
><P
>    (PHP 3 &#62;= 3.0.3, PHP 4, PHP 5)</P
>usort&nbsp;--&nbsp;
     Ordena um array pelos valores utilizando uma função de comparação
     definida pelo usuário 
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN13222"
></A
><H2
>Descrição</H2
>void <B
CLASS="methodname"
>usort</B
> ( array array, string cmp_function )<BR
></BR
><P
>&#13;     Essa função irá ordenar um array pelos valores usando uma função de
     classificação definida pelo usuário. Se o array precisar ser ordenado 
     utilizando um critério não trivial, você deve usar essa função.
    </P
><P
>&#13;     A função de comparação deve retornar um inteiro menor, igual ou maior que
     zero se o primeiro argumento for considerado respectivamente menor,
     igual, ou maior que o segundo. 
     </P
><P
>  

     <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
       Se dois elementos são considerados iguais, a ordem deles fica
       indefinida no array resultante.
       Até o PHP 4.0.6 as funções definidas pelo usuário manteriam a ordem
       original desses elementos, mas com o novo algoritmo de ordenação
       introduzido no 4.1.0 esse não é o caso, pois não existe solução para
       fazer isso de modo eficiente.
      </P
></BLOCKQUOTE
></DIV
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN13239"
></A
><P
><B
>Exemplo 1. Exemplo de <B
CLASS="function"
>usort()</B
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">cmp</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$a </font><font color="#007700">== </font><font color="#0000BB">$b</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">0</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return (</font><font color="#0000BB">$a </font><font color="#007700">&lt; </font><font color="#0000BB">$b</font><font color="#007700">) ? -</font><font color="#0000BB">1 </font><font color="#007700">: </font><font color="#0000BB">1</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= array(</font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">, </font><font color="#0000BB">6</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /><br /></font><font color="#0000BB">usort</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#DD0000">"cmp"</font><font color="#007700">);<br /><br />while (list(</font><font color="#0000BB">$chave</font><font color="#007700">, </font><font color="#0000BB">$valor</font><font color="#007700">) = </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"$chave: $valor</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;     Esse exemplo mostraria:
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>0: 1
1: 2
2: 3
3: 5
4: 6</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
> 
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      Obviamente que nesse caso trivial a função <A
HREF="function.sort.html"
><B
CLASS="function"
>sort()</B
></A
>
      seria mais apropriada.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN13249"
></A
><P
><B
>Exemplo 2. 
       Exemplo de <B
CLASS="function"
>usort()</B
> usando um array multi-dimensional
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">cmp</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">strcmp</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#DD0000">"fruta"</font><font color="#007700">], </font><font color="#0000BB">$b</font><font color="#007700">[</font><font color="#DD0000">"fruta"</font><font color="#007700">]);<br />} <br /><br /></font><font color="#0000BB">$frutas</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#DD0000">"fruta"</font><font color="#007700">] = </font><font color="#DD0000">"limoes"</font><font color="#007700">;<br /></font><font color="#0000BB">$frutas</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#DD0000">"fruta"</font><font color="#007700">] = </font><font color="#DD0000">"abacaxis"</font><font color="#007700">;<br /></font><font color="#0000BB">$frutas</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">][</font><font color="#DD0000">"fruta"</font><font color="#007700">] = </font><font color="#DD0000">"goiabas"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">usort</font><font color="#007700">(</font><font color="#0000BB">$frutas</font><font color="#007700">, </font><font color="#DD0000">"cmp"</font><font color="#007700">); <br /><br />while (list(</font><font color="#0000BB">$chave</font><font color="#007700">, </font><font color="#0000BB">$valor</font><font color="#007700">) = </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$frutas</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">frutas</font><font color="#007700">[</font><font color="#DD0000">$chave</font><font color="#007700">]</font><font color="#DD0000">: " </font><font color="#007700">. </font><font color="#0000BB">$valor</font><font color="#007700">[</font><font color="#DD0000">"fruta"</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;     Na ordenação de um array multi-dimensional, $a e $b contêm
     referências para o primeiro índice do array.
    </P
><P
>&#13;     Esse exemplo mostraria:
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>$fruits[0]: abacaxis
$fruits[1]: goiabas
$fruits[2]: limoes</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>  
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN13257"
></A
><P
><B
>Exemplo 3. 
       Exemplo de <B
CLASS="function"
>usort()</B
> usando uma função membro de um
       objeto 
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">TestObj </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$name</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">TestObj</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">= </font><font color="#0000BB">$name</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Essa é a função estática de comparação */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">cmp_obj</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$al </font><font color="#007700">= </font><font color="#0000BB">strtolower</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">name</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$bl </font><font color="#007700">= </font><font color="#0000BB">strtolower</font><font color="#007700">(</font><font color="#0000BB">$b</font><font color="#007700">-&gt;</font><font color="#0000BB">name</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$al </font><font color="#007700">== </font><font color="#0000BB">$bl</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">0</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (</font><font color="#0000BB">$al </font><font color="#007700">&gt; </font><font color="#0000BB">$bl</font><font color="#007700">) ? +</font><font color="#0000BB">1 </font><font color="#007700">: -</font><font color="#0000BB">1</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$a</font><font color="#007700">[] = new </font><font color="#0000BB">TestObj</font><font color="#007700">(</font><font color="#DD0000">"c"</font><font color="#007700">);<br /></font><font color="#0000BB">$a</font><font color="#007700">[] = new </font><font color="#0000BB">TestObj</font><font color="#007700">(</font><font color="#DD0000">"b"</font><font color="#007700">);<br /></font><font color="#0000BB">$a</font><font color="#007700">[] = new </font><font color="#0000BB">TestObj</font><font color="#007700">(</font><font color="#DD0000">"d"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">usort</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, array (</font><font color="#DD0000">"TestObj"</font><font color="#007700">, </font><font color="#DD0000">"cmp_obj"</font><font color="#007700">));<br /><br />foreach (</font><font color="#0000BB">$a </font><font color="#007700">as </font><font color="#0000BB">$item</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#0000BB">$item</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;     Esse exemplo mostraria:
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>b
c
d</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     Veja também <A
HREF="function.uasort.html"
><B
CLASS="function"
>uasort()</B
></A
>,
     <A
HREF="function.uksort.html"
><B
CLASS="function"
>uksort()</B
></A
>, <A
HREF="function.sort.html"
><B
CLASS="function"
>sort()</B
></A
>,
     <A
HREF="function.asort.html"
><B
CLASS="function"
>asort()</B
></A
>,
     <A
HREF="function.arsort.html"
><B
CLASS="function"
>arsort()</B
></A
>,<A
HREF="function.ksort.html"
><B
CLASS="function"
>ksort()</B
></A
>,
     <A
HREF="function.natsort.html"
><B
CLASS="function"
>natsort()</B
></A
>, e <A
HREF="function.rsort.html"
><B
CLASS="function"
>rsort()</B
></A
>.
    </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.uksort.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ref.aspell.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>uksort</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.array.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Aspell [obsoleta]</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>