Home >Backend Development >PHP Tutorial >Summary of common PHP array sorting methods_PHP tutorial
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>
<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).