Home > Backend Development > PHP Tutorial > Code to implement PHP array sorting_PHP tutorial

Code to implement PHP array sorting_PHP tutorial

WBOY
Release: 2016-07-15 13:27:21
Original
858 people have browsed it

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 web page files are treated as ordinary HTML web page files and when editing, you can write PHP using the normal method of editing HTML. PHP stands for: Hypertext Preprocessor (PHP: Hypertext Preprocessor). PHP is completely free. You don’t need to spend money. You can download it freely from the PHP official site (http://www.php.net). PHP complies with the GNU Public License (GPL), under which many popular software such as Linux and Emacs were born. You have unlimited access to the source code and can even add the features you need. PHP can run on most Unix platforms, GUN/Linux and Microsoft Windows platforms. Having said so much, let’s explain about PHP array sorting.

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>
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446536.htmlTechArticleAfter a long period of development of PHP, many users know PHP very well. Here I will share my personal understanding of PHP array sorting , discuss with everyone. PHP is an easy to learn and use server...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template