Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 4987

php-manual-zh-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="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Array 数组函数"
HREF="ref.array.html"><LINK
REL="PREVIOUS"
TITLE="uksort"
HREF="function.uksort.html"><LINK
REL="NEXT"
TITLE="Aspell 函数(已废弃)"
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"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.uksort.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="ref.aspell.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.usort"
></A
>usort</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN13669"
></A
><P
>    (PHP 3 &#62;= 3.0.3, PHP 4, PHP 5)</P
>usort&nbsp;--&nbsp;
     使用用户自定义的比较函数对数组中的值进行排序
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN13672"
></A
><H2
>说明</H2
>bool <B
CLASS="methodname"
>usort</B
> ( array &#38;array, callback cmp_function )<BR
></BR
><P
>&#13;     本函数将用用户自定义的比较函数对一个数组中的值进行排序。如果要排序的数组需要用一种不寻常的标准进行排序,那么应该使用此函数。
    </P
><P
>&#13;     比较函数必须在第一个参数被认为小于,等于或大于第二个参数时分别返回一个小于,等于或大于零的整数。
    </P
><P
>&#13;     <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
       如果两个成员比较结果相同,则它们在排序后的数组中的顺序未经定义。到
       PHP 4.0.6 之前,用户自定义函数将保留这些单元的原有顺序。但是由于在
       4.1.0 中引进了新的排序算法,结果将不是这样了,因为对此没有一个有效的解决方案。
      </P
></BLOCKQUOTE
></DIV
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>本函数为
<CODE
CLASS="parameter"
>array</CODE
> 中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。</P
></BLOCKQUOTE
></DIV
><P
>&#13;     如果成功则返回 <TT
CLASS="constant"
><B
>TRUE</B
></TT
>,失败则返回 <TT
CLASS="constant"
><B
>FALSE</B
></TT
>。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN13695"
></A
><P
><B
>例 1. <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">)<br />{<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;&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 />foreach (</font><font color="#0000BB">$a </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=&gt; </font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"$key: $value</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
>上例将输出:</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
>注意: </B
>
      很明显在这个小例子中用 <A
HREF="function.sort.html"
><B
CLASS="function"
>sort()</B
></A
> 函数更合适。
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN13705"
></A
><P
><B
>例 2. 
       使用多维数组的 <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">)<br />{<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">"fruit"</font><font color="#007700">], </font><font color="#0000BB">$b</font><font color="#007700">[</font><font color="#DD0000">"fruit"</font><font color="#007700">]);<br />}<br /><br /></font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#DD0000">"fruit"</font><font color="#007700">] = </font><font color="#DD0000">"lemons"</font><font color="#007700">;<br /></font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#DD0000">"fruit"</font><font color="#007700">] = </font><font color="#DD0000">"apples"</font><font color="#007700">;<br /></font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">][</font><font color="#DD0000">"fruit"</font><font color="#007700">] = </font><font color="#DD0000">"grapes"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">usort</font><font color="#007700">(</font><font color="#0000BB">$fruits</font><font color="#007700">, </font><font color="#DD0000">"cmp"</font><font color="#007700">);<br /><br />while (list(</font><font color="#0000BB">$key</font><font color="#007700">, </font><font color="#0000BB">$value</font><font color="#007700">) = </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$fruits</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">fruits</font><font color="#007700">[</font><font color="#DD0000">$key</font><font color="#007700">]</font><font color="#DD0000">: " </font><font color="#007700">. </font><font color="#0000BB">$value</font><font color="#007700">[</font><font color="#DD0000">"fruit"</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;       当排序多维数组时,<CODE
CLASS="varname"
>$a</CODE
> 和 <CODE
CLASS="varname"
>$b</CODE
> 包含到数组第一个索引的引用。
      </P
><P
>上例将输出:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons</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="AEN13715"
></A
><P
><B
>例 3. 
       使用对象的成员函数的 <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">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">/* This is the static comparing function: */<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;&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;echo </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
>上例将输出:</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;     参见
     <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
>
     和 <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"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ref.aspell.html"
ACCESSKEY="N"
>下一页</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"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Aspell 函数(已废弃)</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>