Heim > Artikel > Backend-Entwicklung > 代码实现PHP数组排序_PHP教程
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> <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">></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> </li> <li class=""><span>//定义一个数组 </span></li> <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">></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=""><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> </li> <li class=""><span>//定义2个数组 </span></li> <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">></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=""> <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 class="tag-name">BR</span><span class="tag">></span></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> </li> <li class="alt"><span>//定义一个二维数组 </span></li> <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">></font></strong></span><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>