<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >array_multisort</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="array_merge" HREF="function.array-merge.html"><LINK REL="NEXT" TITLE="array_pad" HREF="function.array-pad.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.array-merge.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-pad.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-multisort" ></A >array_multisort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN11494" ></A ><P > (PHP 4, PHP 5)</P >array_multisort -- 对多个数组或多维数组进行排序</DIV ><DIV CLASS="refsect1" ><A NAME="AEN11497" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >array_multisort</B > ( array ar1 [, mixed arg [, mixed ... [, array ...]]] )<BR ></BR ><P > 如果成功则返回 <TT CLASS="constant" ><B >TRUE</B ></TT >,失败则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><P > <B CLASS="function" >array_multisort()</B > 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。 </P ><P > 关联(<A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A >)键名保持不变,但数字键名会被重新索引。 </P ><P > 输入数组被当成一个表的列并以行来排序――这类似于 SQL 的 ORDER BY 子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。 </P ><P > 本函数的参数结构有些不同寻常,但是非常灵活。第一个参数必须是一个数组。接下来的每个参数可以是数组或者是下面列出的排序标志。 </P ><P > 排序顺序标志: <P ></P ><UL ><LI ><P ><TT CLASS="constant" ><B >SORT_ASC</B ></TT > - 按照上升顺序排序</P ></LI ><LI ><P ><TT CLASS="constant" ><B >SORT_DESC</B ></TT > - 按照下降顺序排序</P ></LI ></UL > </P ><P > 排序类型标志: <P ></P ><UL ><LI ><P ><TT CLASS="constant" ><B >SORT_REGULAR</B ></TT > - 将项目按照通常方法比较</P ></LI ><LI ><P ><TT CLASS="constant" ><B >SORT_NUMERIC</B ></TT > - 将项目按照数值比较</P ></LI ><LI ><P ><TT CLASS="constant" ><B >SORT_STRING</B ></TT > - 将项目按照字符串比较</P ></LI ></UL > </P ><P > 每个数组之后不能指定两个同类的排序标志。每个数组后指定的排序标志仅对该数组有效 - 在此之前为默认值 <TT CLASS="constant" ><B >SORT_ASC</B ></TT > 和 <TT CLASS="constant" ><B >SORT_REGULAR</B ></TT >。 </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11546" ></A ><P ><B >例 1. 对多个数组排序</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ar1 </font><font color="#007700">= array(</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#DD0000">"a"</font><font color="#007700">);<br /></font><font color="#0000BB">$ar2 </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"2"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">array_multisort</font><font color="#007700">(</font><font color="#0000BB">$ar1</font><font color="#007700">, </font><font color="#0000BB">$ar2</font><font color="#007700">);<br /><br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$ar1</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$ar2</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 本例中经过排序后,第一个数组将包含 "10","a",100,100。第二个数组将包含 1,1,"2",3。第二个数组中的项目顺序完全和第一个数组中相应的项目(100 和 100)顺序一致。 </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(4) { [0]=> string(2) "10" [1]=> string(1) "a" [2]=> int(100) [3]=> int(100) } array(4) { [0]=> int(1) [1]=> int(1) [2]=> string(1) "2" [3]=> int(3) }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11552" ></A ><P ><B >例 2. 对多维数组排序</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ar </font><font color="#007700">= array (array (</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#DD0000">"a"</font><font color="#007700">), array (</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"2"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">));<br /></font><font color="#0000BB">array_multisort </font><font color="#007700">(</font><font color="#0000BB">$ar</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">SORT_ASC</font><font color="#007700">, </font><font color="#0000BB">SORT_STRING</font><font color="#007700">,<br /> </font><font color="#0000BB">$ar</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">SORT_NUMERIC</font><font color="#007700">, </font><font color="#0000BB">SORT_DESC</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 本例中经过排序后,第一个数组将包含 10,100,100,"a"(作为字符串上升排序),第二个数组将包含 1,3,"2",1(作为数值下降排序)。 </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11557" ></A ><P ><B >例 3. Sorting multi-dimensional array</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ar </font><font color="#007700">= array(<br /> array(</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">11</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">, </font><font color="#DD0000">"a"</font><font color="#007700">),<br /> array( </font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#DD0000">"2"</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">)<br /> );<br /></font><font color="#0000BB">array_multisort</font><font color="#007700">(</font><font color="#0000BB">$ar</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">SORT_ASC</font><font color="#007700">, </font><font color="#0000BB">SORT_STRING</font><font color="#007700">,<br /> </font><font color="#0000BB">$ar</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">SORT_NUMERIC</font><font color="#007700">, </font><font color="#0000BB">SORT_DESC</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$ar</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 本例中在排序后,第一个数组将变成 "10",100,100,11,"a"(被当作字符串以升序排列)。第二个数组将包含 1, 3, "2", 2, 1(被当作数字以降序排列)。 </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { [0]=> array(5) { [0]=> string(2) "10" [1]=> int(100) [2]=> int(100) [3]=> int(11) [4]=> string(1) "a" } [1]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> string(1) "2" [3]=> int(2) [4]=> int(1) } }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11563" ></A ><P ><B >例 4. 对数据库结果进行排序</B ></P ><P > 本例中 <CODE CLASS="varname" >data</CODE > 数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。 </P ><P > 例子中的数据如下: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >volume | edition -------+-------- 67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7</PRE ></TD ></TR ></TABLE ><P > 数据全都存放在名为 <CODE CLASS="varname" >data</CODE > 的数组中。这通常是通过循环从数据库取得的结果,例如 <A HREF="function.mysql-fetch-assoc.html" ><B CLASS="function" >mysql_fetch_assoc()</B ></A >。 </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">67</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">2</font><font color="#007700">);<br /></font><font color="#0000BB">$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">86</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">85</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">6</font><font color="#007700">);<br /></font><font color="#0000BB">$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">98</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">2</font><font color="#007700">);<br /></font><font color="#0000BB">$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">86</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">6</font><font color="#007700">);<br /></font><font color="#0000BB">$data</font><font color="#007700">[] = array(</font><font color="#DD0000">'volume' </font><font color="#007700">=> </font><font color="#0000BB">67</font><font color="#007700">, </font><font color="#DD0000">'edition' </font><font color="#007700">=> </font><font color="#0000BB">7</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 本例中将把 <CODE CLASS="varname" >volume</CODE > 降序排列,把 <CODE CLASS="varname" >edition</CODE > 升序排列。 </P ><P > 现在有了包含有行的数组,但是 <B CLASS="function" >array_multisort()</B > 需要一个包含列的数组,因此用以下代码来取得列,然后排序。 </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// 取得列的列表<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$data </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=> </font><font color="#0000BB">$row</font><font color="#007700">) {<br /> </font><font color="#0000BB">$volume</font><font color="#007700">[</font><font color="#0000BB">$key</font><font color="#007700">] = </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'volume'</font><font color="#007700">];<br /> </font><font color="#0000BB">$edition</font><font color="#007700">[</font><font color="#0000BB">$key</font><font color="#007700">] = </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'edition'</font><font color="#007700">];<br />}<br /><br /></font><font color="#FF8000">// 将数据根据 volume 降序排列,根据 edition 升序排列<br />// 把 $data 作为最后一个参数,以通用键排序<br /></font><font color="#0000BB">array_multisort</font><font color="#007700">(</font><font color="#0000BB">$volume</font><font color="#007700">, </font><font color="#0000BB">SORT_DESC</font><font color="#007700">, </font><font color="#0000BB">$edition</font><font color="#007700">, </font><font color="#0000BB">SORT_ASC</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 数据集合现在排好序了,结果如下: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11582" ></A ><P ><B >例 5. 不区分大小写字母排序</B ></P ><P > <TT CLASS="constant" ><B >SORT_STRING</B ></TT > 和 <TT CLASS="constant" ><B >SORT_REGULAR</B ></TT > 都是区分大小写字母的,大写字母会排在小写字母之前。 </P ><P > 要进行不区分大小写的排序,就要按照原数组的小写字母拷贝来排序。 </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$array </font><font color="#007700">= array(</font><font color="#DD0000">'Alpha'</font><font color="#007700">, </font><font color="#DD0000">'atomic'</font><font color="#007700">, </font><font color="#DD0000">'Beta'</font><font color="#007700">, </font><font color="#DD0000">'bank'</font><font color="#007700">);<br /></font><font color="#0000BB">$array_lowercase </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">'strtolower'</font><font color="#007700">, </font><font color="#0000BB">$array</font><font color="#007700">);<br /><br /></font><font color="#0000BB">array_multisort</font><font color="#007700">(</font><font color="#0000BB">$array_lowercase</font><font color="#007700">, </font><font color="#0000BB">SORT_ASC</font><font color="#007700">, </font><font color="#0000BB">SORT_STRING</font><font color="#007700">, </font><font color="#0000BB">$array</font><font color="#007700">);<br /><br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Alpha [1] => atomic [2] => bank [3] => Beta )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 【译者注】本函数相当有用,为有助于理解,请再看下面这个例子: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11593" ></A ><P ><B >例 6. 名次排列</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$grade </font><font color="#007700">= array(</font><font color="#DD0000">"score" </font><font color="#007700">=> array(</font><font color="#0000BB">70</font><font color="#007700">, </font><font color="#0000BB">95</font><font color="#007700">, </font><font color="#0000BB">70.0</font><font color="#007700">, </font><font color="#0000BB">60</font><font color="#007700">, </font><font color="#DD0000">"70"</font><font color="#007700">),<br /> </font><font color="#DD0000">"name" </font><font color="#007700">=> array(</font><font color="#DD0000">"Zhang San"</font><font color="#007700">, </font><font color="#DD0000">"Li Si"</font><font color="#007700">, </font><font color="#DD0000">"Wang Wu"</font><font color="#007700">,<br /> </font><font color="#DD0000">"Zhao Liu"</font><font color="#007700">, </font><font color="#DD0000">"Liu Qi"</font><font color="#007700">));<br /></font><font color="#0000BB">array_multisort</font><font color="#007700">(</font><font color="#0000BB">$grade</font><font color="#007700">[</font><font color="#DD0000">"score"</font><font color="#007700">], </font><font color="#0000BB">SORT_NUMERIC</font><font color="#007700">, </font><font color="#0000BB">SORT_DESC</font><font color="#007700">,<br /> </font><font color="#FF8000">// 将分数作为数值,由高到低排序<br /> </font><font color="#0000BB">$grade</font><font color="#007700">[</font><font color="#DD0000">"name"</font><font color="#007700">], </font><font color="#0000BB">SORT_STRING</font><font color="#007700">, </font><font color="#0000BB">SORT_ASC</font><font color="#007700">);<br /> </font><font color="#FF8000">// 将名字作为字符串,由小到大排序<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$grade</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { ["score"]=> array(5) { [0]=> int(95) [1]=> string(2) "70" [2]=> float(70) [3]=> int(70) [4]=> int(60) } ["name"]=> array(5) { [0]=> string(5) "Li Si" [1]=> string(6) "Liu Qi" [2]=> string(7) "Wang Wu" [3]=> string(9) "Zhang San" [4]=> string(8) "Zhao Liu" } }</PRE ></TD ></TR ></TABLE ><P > 本例中对包含成绩的数组 <CODE CLASS="varname" >$grade</CODE > 按照分数(score)由高到低进行排序,分数相同的人则按照名字(name)由小到大排序。排序后李四 95 分为第一名,赵六 60 分为第五名没有异议。张三、王五和刘七都是 70 分,他们的名次则由其姓名的字母顺序排列,Liu 在前,Wang 在后而 Zhang 在最后。为了区别,三个 70 分分别用了整数,浮点数和字符串来表示,可以在程序输出中清楚地看到它们排序的结果。 </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-merge.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="function.array-pad.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_merge</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_pad</TD ></TR ></TABLE ></DIV ></BODY ></HTML >