📄 function.uksort.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Sort an array by keys using a user-defined comparison function</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.uasort.html">uasort</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.usort.html">usort</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.uksort" class="refentry"> <div class="refnamediv"> <h1 class="refname">uksort</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">uksort</span> — <span class="dc-title">Sort an array by keys using a user-defined comparison function</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>uksort</b></b></span> ( <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$array</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$cmp_function</tt></span> )</div> <p class="para rdfs-comment"> <b>uksort()</b> will sort the keys of an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. </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">cmp_function</tt></i></span> <dd> <p class="para"> The callback comparison function. </p> <p class="para"> Function <i><tt class="parameter">cmp_function</tt></i> should accept two parameters which will be filled by pairs of <i><tt class="parameter">array</tt></i> keys. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </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 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>uksort()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">cmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'^(a|an|the) '</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'^(a|an|the) '</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">strcasecmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"John" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"the Earth" </span><span style="color: #007700">=> </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"an apple" </span><span style="color: #007700">=> </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"a banana" </span><span style="color: #007700">=> </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">uksort</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #DD0000">"cmp"</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$a </span><span style="color: #007700">as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"$key: $value\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></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>an apple: 3a banana: 4the Earth: 2John: 1</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.usort.html" class="function" rel="rdfs-seeAlso">usort()</a></li> <li class="member"><a href="function.uasort.html" class="function" rel="rdfs-seeAlso">uasort()</a></li> <li class="member"><a href="function.sort.html" class="function" rel="rdfs-seeAlso">sort()</a></li> <li class="member"><a href="function.asort.html" class="function" rel="rdfs-seeAlso">asort()</a></li> <li class="member"><a href="function.arsort.html" class="function" rel="rdfs-seeAlso">arsort()</a></li> <li class="member"><a href="function.ksort.html" class="function" rel="rdfs-seeAlso">ksort()</a></li> <li class="member"><a href="function.natsort.html" class="function" rel="rdfs-seeAlso">natsort()</a></li> <li class="member"><a href="function.rsort.html" class="function" rel="rdfs-seeAlso">rsort()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.uasort.html">uasort</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.usort.html">usort</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -