首页 > 后端开发 > php教程 > PHP函数sort()如何实现数组排序_PHP教程

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

WBOY
发布: 2016-07-15 13:28:54
原创
925 人浏览过

我们在学习一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为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...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板