Home  >  Article  >  Backend Development  >  Summary of common PHP array sorting methods_PHP tutorial

Summary of common PHP array sorting methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:01792browse

With the rapid development of PHP, more and more people are using it. In the PHP array learning excerpt, you can learn about the establishment of the most basic PHP array and the display of array elements. You need to learn in depth the related operations of PHP arrays. The first thing I came into contact with was the problem of PHP array sorting and descending order.

The function range() to quickly create an array

<ol class="dp-xml">
<li class="alt"><span><span>比如range()函数可以快速创建从1到9的数字数组:  </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><SPAN class=attribute><FONT color=#ff0000>numbers</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>range</FONT></SPAN><SPAN>(1,9);  </SPAN></SPAN><LI class=""><SPAN>echo $numbers[1];  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong><span> </span>
</li>
</ol>

Of course, using range(9,1) creates an array of numbers from 9 to 1. At the same time, range() can also create a character array from a to z:

<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>numbers</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>range</FONT></SPAN><SPAN>(a,z);  </SPAN></SPAN><LI class=alt><SPAN>foreach ($numbers as $mychrs)  </SPAN><LI class=""><SPAN>echo $mychrs." ";  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>

Pay attention to the case when using character arrays, such as range(A, z) and range(a,Z) are different. The range() function also has a third parameter, which is used to set the step size. For example, the array elements created by range(1,9,3) are: 1, 4, 7. Common PHP array sorting Generally, each element in the array is represented by characters or numbers, so the array elements can be arranged in ascending order. This function is sort(). For example:
<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>people</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('name','sex','nation','birth');  </SPAN></SPAN><LI class=alt><SPAN>foreach ($people as $mychrs)  </SPAN><LI class=""><SPAN>echo $mychrs." ";  </SPAN><LI class=alt><SPAN>sort($people);  </SPAN><LI class=""><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong></span><span>---排序后---</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>";  </span>
</li>
<li class="alt"><span>foreach ($people as $mychrs)  </span></li>
<li class=""><span>echo $mychrs." ";  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

The array elements sorted in ascending order are displayed as birth name nation sex. Of course, the sort() function is case-sensitive (the order of letters from large to small is: A...Z... a...z)

The Sort() function also has a second parameter, which is used to indicate whether the PHP array sorting rule in ascending order is used to compare numbers or strings. For example:

<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>echo "---按数字升序排序---</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>";  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">num2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('26','3',);  </span>
</li>
<li class=""><span>sort($num2,SORT_NUMERIC);  </span></li>
<li class="alt"><span>foreach ($num2 as $mychrs)  </span></li>
<li class=""><span>echo $mychrs." ";  </span></li>
<li class="alt">
<span>echo "</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>---按字符升序排序---</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>";  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">num3</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('26','3');  </span>
</li>
<li class="alt"><span>sort($num3,SORT_STRING);  </span></li>
<li class=""><span>foreach ($num3 as $mychrs)  </span></li>
<li class="alt"><span>echo $mychrs." ";  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

SORT_NUMERIC and SORT_STRING are used to declare ascending order of numbers or characters. If arranged in ascending order of numbers, it is: 3, 26; but if arranged in ascending order of characters, it is: 26, 3. In addition to the ascending function in PHP, there is also a descending or reverse sorting function, which is the rsort() function. For example: $num1=range(1,9); rsort($num1); here is actually equivalent to range(9 ,1).


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446476.htmlTechArticleWith the rapid development of PHP, more and more people are using it. Learn about it in the excerpt of PHP array learning The most basic PHP array creation and display of array elements. Need to learn more about PHP arrays...
Statement:
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