Home >Backend Development >PHP Tutorial >How to implement array sorting with PHP function sort()_PHP tutorial
We are learningGenerally, each element in an 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 PHP function sort() is case-sensitive (letters from large to small are: A...Z...a...z)
The Sort() function also has a second parameter, which is used to indicate whether the ascending order rule 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> </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><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">num3</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('26','3'); </span> </li> <li class=""><span>sort($num3,SORT_STRING); </span></li> <li class="alt"><span>foreach ($num3 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>
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 order function, there is also a descending or reverse sorting function in PHP, which is the PHP function sort(), for example:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">num1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">range</font></span><span>(1,9); </span></span></li> <li class=""><span>rsort($num1); //这里其实就相当于range(9,1) </span></li> </ol>