PHP 数组排序
<div id="article_content" class="article_content clearfix"><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
<div id="content_views" class="htmledit_views">
<p>数组中的元素可以按字母或数字顺序进行降序或升序排列。</p>
<hr>
<h2>PHP - 数组排序函数</h2>
<p>在本章中,我们将一一介绍下列 PHP 数组排序函数:</p>
<ul><li>sort() - 对数组进行升序排列</li><li>rsort() - 对数组进行降序排列</li><li>asort() - 根据关联数组的值,对数组进行升序排列</li><li>ksort() - 根据关联数组的键,对数组进行升序排列</li><li>arsort() - 根据关联数组的值,对数组进行降序排列</li><li>krsort() - 根据关联数组的键,对数组进行降序排列</li></ul>
<hr>
<h2>sort() - 对数组进行升序排列</h2>
<p>下面的实例将 $cars 数组中的元素按照字母升序排列:</p>
<h2>实例</h2>
<p><?php<br> $cars=array("Volvo","BMW","Toyota");<br> sort($cars);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_sort_alpha">运行实例 »</a></p>
<p>下面的实例将 $numbers 数组中的元素按照数字升序排列:</p>
<h2>实例</h2>
<p><?php<br> $numbers=array(4,6,2,22,11);<br> sort($numbers);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_sort_num">运行实例 »</a></p>
<p></p>
<hr>
<h2>rsort() - 对数组进行降序排列</h2>
<p>下面的实例将 $cars 数组中的元素按照字母降序排列:</p>
<h2>实例</h2>
<p><?php<br> $cars=array("Volvo","BMW","Toyota");<br> rsort($cars);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_rsort_alpha">运行实例 »</a></p>
<p>下面的实例将 $numbers 数组中的元素按照数字降序排列:</p>
<h2>实例</h2>
<p><?php<br> $numbers=array(4,6,2,22,11);<br> rsort($numbers);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_rsort_num">运行实例 »</a></p>
<p></p>
<hr>
<h2>asort() - 根据数组的值,对数组进行升序排列</h2>
<p>下面的实例根据数组的值,对关联数组进行升序排列:</p>
<h2>实例</h2>
<p><?php<br> $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");<br> asort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_asort">运行实例 »</a></p>
<p></p>
<hr>
<h2>ksort() - 根据数组的键,对数组进行升序排列</h2>
<p>下面的实例根据数组的键,对关联数组进行升序排列:</p>
<h2>实例</h2>
<p><?php<br> $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");<br> ksort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_ksort">运行实例 »</a></p>
<p></p>
<hr>
<h2>arsort() - 根据数组的值,对数组进行降序排列</h2>
<p>下面的实例根据数组的值,对关联数组进行降序排列:</p>
<h2>实例</h2>
<p><?php<br> $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");<br> arsort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_array_arsort">运行实例 »</a></p>
<p></p>
<hr>
<h2>krsort() - 根据数组的键,对数组进行降序排列</h2>
<p>下面的实例根据数组的键,对关联数组进行降序排列:</p>
<h2>实例</h2>
<p><?php<br> $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");<br> krsort($age);<br> ?></p>
</div>
</div>
<div id="treeSkill"></div>
页:
[1]