admin 发表于 2023-2-16 18:54:41

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>在本章中&#xff0c;我们将一一介绍下列 PHP 数组排序函数&#xff1a;</p>
<ul><li>sort() - 对数组进行升序排列</li><li>rsort() - 对数组进行降序排列</li><li>asort() - 根据关联数组的值&#xff0c;对数组进行升序排列</li><li>ksort() - 根据关联数组的键&#xff0c;对数组进行升序排列</li><li>arsort() - 根据关联数组的值&#xff0c;对数组进行降序排列</li><li>krsort() - 根据关联数组的键&#xff0c;对数组进行降序排列</li></ul>
<hr>
<h2>sort() - 对数组进行升序排列</h2>
<p>下面的实例将 $cars 数组中的元素按照字母升序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $cars&#61;array(&#34;Volvo&#34;,&#34;BMW&#34;,&#34;Toyota&#34;);<br> sort($cars);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_sort_alpha">运行实例 »</a></p>
<p>下面的实例将 $numbers 数组中的元素按照数字升序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $numbers&#61;array(4,6,2,22,11);<br> sort($numbers);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_sort_num">运行实例 »</a></p>
<p></p>
<hr>
<h2>rsort() - 对数组进行降序排列</h2>
<p>下面的实例将 $cars 数组中的元素按照字母降序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $cars&#61;array(&#34;Volvo&#34;,&#34;BMW&#34;,&#34;Toyota&#34;);<br> rsort($cars);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_rsort_alpha">运行实例 »</a></p>
<p>下面的实例将 $numbers 数组中的元素按照数字降序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $numbers&#61;array(4,6,2,22,11);<br> rsort($numbers);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_rsort_num">运行实例 »</a></p>
<p></p>
<hr>
<h2>asort() - 根据数组的值&#xff0c;对数组进行升序排列</h2>
<p>下面的实例根据数组的值&#xff0c;对关联数组进行升序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $age&#61;array(&#34;Peter&#34;&#61;>&#34;35&#34;,&#34;Ben&#34;&#61;>&#34;37&#34;,&#34;Joe&#34;&#61;>&#34;43&#34;);<br> asort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_asort">运行实例 »</a></p>
<p></p>
<hr>
<h2>ksort() - 根据数组的键&#xff0c;对数组进行升序排列</h2>
<p>下面的实例根据数组的键&#xff0c;对关联数组进行升序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $age&#61;array(&#34;Peter&#34;&#61;>&#34;35&#34;,&#34;Ben&#34;&#61;>&#34;37&#34;,&#34;Joe&#34;&#61;>&#34;43&#34;);<br> ksort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_ksort">运行实例 »</a></p>
<p></p>
<hr>
<h2>arsort() - 根据数组的值&#xff0c;对数组进行降序排列</h2>
<p>下面的实例根据数组的值&#xff0c;对关联数组进行降序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $age&#61;array(&#34;Peter&#34;&#61;>&#34;35&#34;,&#34;Ben&#34;&#61;>&#34;37&#34;,&#34;Joe&#34;&#61;>&#34;43&#34;);<br> arsort($age);<br> ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_array_arsort">运行实例 »</a></p>
<p></p>
<hr>
<h2>krsort() - 根据数组的键&#xff0c;对数组进行降序排列</h2>
<p>下面的实例根据数组的键&#xff0c;对关联数组进行降序排列&#xff1a;</p>
<h2>实例</h2>
<p><?php<br> $age&#61;array(&#34;Peter&#34;&#61;>&#34;35&#34;,&#34;Ben&#34;&#61;>&#34;37&#34;,&#34;Joe&#34;&#61;>&#34;43&#34;);<br> krsort($age);<br> ?></p>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: PHP 数组排序