首頁 >後端開發 >php教程 >代码实现PHP数组排序_PHP教程

代码实现PHP数组排序_PHP教程

WBOY
WBOY原創
2016-07-15 13:27:21865瀏覽

PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解PHP数组排序,和大家讨论讨论。PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的WEB站点。本教程并不想让你完全了解这种语言,只是能使你尽快加入开发动态web站点的行列。我假定你有一些HTML(或者HTML编辑器)的基本知识和一些编程思想。

PHP网页文件被当作一般HTML网页文件来处理并且在编辑时你可以用编辑HTML的常规方法编写PHP。 PHP代表:超文本预处理器(PHP: Hypertext Preprocessor)。PHP是完全免费的,不用花钱,你可以从PHP官方站点(http://www.php.net)自由下载。PHP遵守GNU公共许可(GPL),在这一许可下诞生了许多流行的软件诸如Linux和Emacs。你可以不受限制的获得源码,甚至可以从中加进你自己需要的特色。PHP在大多数Unix平台,GUN/Linux和微软Windows平台上均可以运行。说了这么多,下面我们就讲解一下关于PHP数组排序。

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>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446536.htmlTechArticlePHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解PHP数组排序,和大家讨论讨论。PHP是一种易于学习和使用的服务器...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn