PHP の長い開発期間を経て、多くのユーザーが PHP をよく知っています。ここでは、PHP の配列ソートに関する私の個人的な理解を共有し、皆さんと議論します。 PHP は、習得と使用が簡単なサーバーサイド スクリプト言語です。プログラミングの知識がほとんどなくても、PHP を使用して真にインタラクティブな WEB サイトを構築できます。このチュートリアルは、言語を完全に理解することを目的としたものではなく、動的な Web サイトの開発をできるだけ早く開始できるようにすることを目的としています。 HTML (または HTML エディター) の基本的な知識とプログラミングのアイデアがあることを前提としています。
PHP 配列ソート コード:
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>arr</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(</SPAN><SPAN class=attribute><FONT color=#ff0000>5</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>"zero",</span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"one",</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"two");//定义一个数组 </span> </li> <li class="alt"><span>sort($arr);//使用sort对数组进行排序 </span></li> <li class=""> <span>foreach($arras$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value)//对数组进行遍历查看排序后的结果 </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>echo"Key:$key;Value:$value;"; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>//定义一个数组 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>arr</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(</SPAN><SPAN class=attribute><FONT color=#ff0000>5</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>"zero",</span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"one",</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"two"); </span> </li> <li class=""><span>//使用rsort对数组进行排序 </span></li> <li class="alt"><span>rsort($arr); </span></li> <li class=""><span>//对数组进行遍历查看排序后的结果 </span></li> <li class="alt"> <span>foreach($arras$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value){ </span> </li> <li class=""><span>echo"Key:$key;Value:$value;"; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>//定义2个数组 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ar1</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(</SPAN><SPAN class=attribute><FONT color=#ff0000>5</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>"zero",</span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"one",</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"two"); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ar2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"three",</span><span class="attribute"><font color="#ff0000">9</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"four",</span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"five"); </span> </li> <li class="alt"><span>//对数组进行排序 </span></li> <li class=""><span>array_multisort($ar1,$ar2); </span></li> <li class="alt"><span>//对数组进行遍历查看排序后的结果 </span></li> <li class=""> <span>foreach($ar1as$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value){ </span> </li> <li class="alt"><span>echo"Key:$key;Value:$value;"; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>echo"</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>BR</SPAN><SPAN class=tag>></span></font></strong><span>"; </span> </li> <li class=""> <span>foreach($ar2as$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value){ </span> </li> <li class="alt"><span>echo"Key:$key;Value:$value;"; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>//定义一个二维数组 </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>array</FONT></SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>=array("ar1"=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>array(</span><span class="attribute"><font color="#ff0000">5</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100,</span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>120,</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>30), </span> </li> <li class="alt"> <span>"ar2"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array(</span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"three",</span><span class="attribute"><font color="#ff0000">9</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"four",</span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"five")); </span> </li> <li class=""><span>//对数组进行排序 </span></li> <li class="alt"><span>array_multisort($array["ar1"],SORT_NUMERIC,SORT_DESC, </span></li> <li class=""><span>$array["ar2"],SORT_STRING,SORT_ASC); </span></li> <li class="alt"><span>//对数组进行遍历查看排序后的结果 </span></li> <li class=""><span>foreach($arrayas$v1) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>foreach($v1as$v2) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>echo"$v2n"; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>