After a long period of development of PHP, many users know PHP very well. Here I will share my personal understanding of PHP array sorting and discuss it with everyone. PHP is a server-side scripting language that is easy to learn and use. With very little programming knowledge you can use PHP to build a truly interactive WEB site. This tutorial is not intended to give you a complete understanding of the language, but to get you started developing dynamic web sites as quickly as possible. I assume you have some basic knowledge of HTML (or an HTML editor) and some programming ideas.
PHP array sorting code:
<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>