function.sort.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 205 行

HTML
205
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Sort an array</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sizeof.html">sizeof</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.uasort.html">uasort</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.sort" class="refentry"> <div class="refnamediv">  <h1 class="refname">sort</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">sort</span> &mdash; <span class="dc-title">Sort an array</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">bool</span> <span class="methodname"><b><b>sort</b></b></span>    ( <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$array</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$sort_flags</tt></span>  ] )</div>  <p class="para rdfs-comment">   This function sorts an array.  Elements will be arranged from   lowest to highest when this function has completed.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">array</tt></i></span>     <dd>      <p class="para">       The input array.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">sort_flags</tt></i></span>     <dd>      <p class="para">       The optional second parameter <i><tt class="parameter">sort_flags</tt></i>       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"><b><tt>SORT_REGULAR</tt></b> - compare items normally         (don&#039;t change types)</span>        </li>        <li class="listitem">         <span class="simpara"><b><tt>SORT_NUMERIC</tt></b> - compare items numerically</span>        </li>        <li class="listitem">         <span class="simpara"><b><tt>SORT_STRING</tt></b> - compare items as strings</span>        </li>        <li class="listitem">         <span class="simpara"><b><tt>SORT_LOCALE_STRING</tt></b> - compare items as          strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2.          Before PHP 6, it uses the system locale, which can be changed using          <a href="function.setlocale.html" class="function">setlocale()</a>. Since PHP 6, you must use the          <b>i18n_loc_set_default()</b> function.         </span>        </li>       </ul>      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.0.0</td>       <td colspan="1" rowspan="1" align="left">        The <i><tt class="parameter">sort_flags</tt></i> parameter was added.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>sort()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"fruits["&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"]&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>fruits[0] = applefruits[1] = bananafruits[2] = lemonfruits[3] = orange</pre></div>    </pre></div>   </div>  </p>  <p class="para">   The fruits have been sorted in alphabetical order.  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>: <span class="simpara">This functionassigns new keys to the elements in <i><tt class="parameter">array</tt></i>.It will remove any existing keys that may have been assigned, ratherthan just reordering the keys.</span></p></blockquote>  <blockquote><p><b class="note">Note</b>:    <span class="simpara">    Like most PHP sorting functions, <b>sort()</b> uses an    implementation of <a href="http://en.wikipedia.org/wiki/Quicksort" class="link external">&raquo; Quicksort</a>.   </span>  </p></blockquote>  <div class="warning"><b class="warning">Warning</b>   <p class="simpara">    Be careful when sorting arrays with mixed types values because    <b>sort()</b> can produce unpredictable results.   </p>  </div> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.arsort.html" class="function" rel="rdfs-seeAlso">arsort()</a></li>    <li class="member"><a href="function.asort.html" class="function" rel="rdfs-seeAlso">asort()</a></li>    <li class="member"><a href="function.ksort.html" class="function" rel="rdfs-seeAlso">ksort()</a></li>    <li class="member"><a href="function.rsort.html" class="function" rel="rdfs-seeAlso">rsort()</a></li>    <li class="member"><a href="function.usort.html" class="function" rel="rdfs-seeAlso">usort()</a></li>    <li class="member"><a href="function.uksort.html" class="function" rel="rdfs-seeAlso">uksort()</a></li>    <li class="member"><a href="function.array-multisort.html" class="function" rel="rdfs-seeAlso">array_multisort()</a></li>    <li class="member"><a href="function.krsort.html" class="function" rel="rdfs-seeAlso">krsort()</a></li>    <li class="member"><a href="function.natsort.html" class="function" rel="rdfs-seeAlso">natsort()</a></li>    <li class="member"><a href="function.natcasesort.html" class="function" rel="rdfs-seeAlso">natcasesort()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sizeof.html">sizeof</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.uasort.html">uasort</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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?