Heim  >  Artikel  >  Backend-Entwicklung  >  PHP函数sort()如何实现数组排序_PHP教程

PHP函数sort()如何实现数组排序_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:28:54886Durchsuche

我们在学习一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如:

<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">people</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('name','sex','nation','birth');  </span>
</li>
<li class="alt"><span>foreach ($people as $mychrs)  </span></li>
<li class=""><span>     echo $mychrs." ";  </span></li>
<li class="alt"><span>sort($people);  </span></li>
<li class="">
<span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>---排序后---</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><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>

升序排序后的数组元素显示为 birth name nation sex,当然,PHP函数sort()是区分字母大小写的(字母从大到小的顺序是:A…Z…a…z)

Sort()函数还具有第二参数,用来说明升序的规则是用来比较数字还是字符串的。比如:

<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 class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><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 class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>---按字符升序排序---</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><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和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。

PHP中除了升序函数以外,还有降序或称反向排列的函数,就是PHP函数sort(),比如:

<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>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446405.htmlTechArticle我们在学习 一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如: ? php $ people = array...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn