Heim > Backend-Entwicklung > PHP-Tutorial > 关于常见PHP数组排序方法总结_PHP教程

关于常见PHP数组排序方法总结_PHP教程

WBOY
Freigeben: 2016-07-15 13:28:01
Original
851 Leute haben es durchsucht

随着PHP的快速发展,用它的人越来越多,在PHP数组学习摘录部分了解到最基本的PHP数组的建立和数组元素的显示。需要深入学习下PHP数组的相关操作。首先接触的就是PHP数组排序、降序的排序问题。

快速创建数组的函数range()

<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>
</li>
<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>
</li>
<li class=""><span>echo $numbers[1];  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Nach dem Login kopieren

当然,使用range(9,1)则创建了9到1的数字数组。同时,range()还可以创建从a到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>
<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>
</li>
<li class="alt"><span>foreach ($numbers 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>
Nach dem Login kopieren

使用字符数组时注意大小写,比如range(A,z)和range(a,Z)是不一样的。range()函数还具有第三个参数,该参数的作用是设定步长,比如range(1,9,3)创建的数组元素是:1、4、7。常见PHP数组排序一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为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>
Nach dem Login kopieren

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

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

<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>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="">
<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>
Nach dem Login kopieren

SORT_NUMERIC和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如:$num1=range(1,9);rsort($num1);这里其实就相当于range(9,1)。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446476.htmlTechArticle随着PHP的快速发展,用它的人越来越多,在PHP数组学习摘录部分了解到最基本的PHP数组的建立和数组元素的显示。需要深入学习下PHP数组的...
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage