php 数组排序(升序、降序及相关问题)

WBOY
Freigeben: 2016-07-25 09:04:05
Original
2046 Leute haben es durchsucht
  1. $a = array(4,"37",3,100,0,-5);
  2. sort($a);
  3. for ($i=0; $iecho $a[$i]." ";
  4. }
  5. echo "
    ";
  6. sort($a,SORT_STRING);
  7. for ($i=0; $iecho $a[$i]." ";
  8. }
  9. echo "
    ";
  10. ?>
复制代码

输出结果: -5 0 3 4 37 100 -5 0 100 3 37 4

降序排序:rsort(array, [sort type]) 参数用法与sort函数相同。

关联数组排序: 函数:asort(array, [sort type]) 说明:根据关联数组的元素值进行升序排序。参数使用见上面的sort函数。

函数:ksort(array, [sort type]) 说明:根据关联数组的关键字进行升序排序。参数使用见上面的sort函数。

  1. $a = array(

  2. "good" => "bad",
  3. "right" => "wrong",
  4. "boy" => "girl");
  5. echo "value sort
    ";

  6. asort($a);
  7. foreach($a as $key => $value){
  8. echo "$key : $value
    ";
  9. }
  10. echo "
    key sort
    ";

  11. ksort($a);
  12. foreach($a as $key => $value){
  13. echo "$key : $value
    ";
  14. }
  15. ?>
复制代码

输出结果: value sort good : bad boy : girl right : wrong

key sort boy : girl good : bad right : wrong 降序排序: arsort(array, [sort type]) 与 asort对应 krsort(array, [sort type]) 与 ksort对应

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

比如range()函数可以快速创建从1到9的数字数组:

  1. $numbers=range(1,9);
  2. echo $numbers[1];
  3. ?>
复制代码

当然,使用range(9,1)则创建了9到1的数字数组。同时,range()还可以创建从a到z 的字符数组:

  1. $numbers=range(a,z);
  2. foreach ($numbers as $mychrs)
  3. echo $mychrs." ";
  4. ?>
复制代码

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

  1. $people=array('name','sex','nation','birth');
  2. foreach ($people as $mychrs)
  3. echo $mychrs." ";
  4. sort($people);
  5. echo "
    ---排序后---
    ";
  6. foreach ($people as $mychrs)
  7. echo $mychrs." ";
  8. ?>
复制代码

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

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

  1. echo "---按数字升序排序---
    ";
  2. $num2=array('26','3',);
  3. sort($num2,SORT_NUMERIC);
  4. foreach ($num2 as $mychrs)
  5. echo $mychrs." ";
  6. echo "
    ---按字符升序排序---
    ";
  7. $num3=array('26','3');
  8. sort($num3,SORT_STRING);
  9. foreach ($num3 as $mychrs)
  10. echo $mychrs." ";
  11. ?>
复制代码

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

有关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
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!