加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱资讯网 (https://www.52junxun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP数组排序函数有哪些

发布时间:2022-11-14 11:05:07 所属栏目:PHP教程 来源:
导读:  PHP数组排序函数有:1、sort函数;2、rsort函数;3、asort函数;4、ksort函数;5、arsort函数;6、krsort函数等等。

  PHP数组排序函数

  1、使用sort()

  sort() 函数对数值数组进行升序排序。
  PHP数组排序函数有:1、sort函数;2、rsort函数;3、asort函数;4、ksort函数;5、arsort函数;6、krsort函数等等。

  PHP数组排序函数
 
  1、使用sort()
 
  sort() 函数对数值数组进行升序排序。
 
  ";
     }
  ?>
  输出:
 
  BMW
  Toyota
  Volvo
  2、使用rsort() 函数
 
  rsort() 函数对数值数组进行降序排序。
 
  ";
  }
  ?>
  输出:
 
  Volvo
  Toyota
  BMW
  3、使用asort()
 
  asort() 函数对关联数组按照键值进行降序排序。
 
  "35","Ben"=>"37","Joe"=>"43");
  asort($age);
  foreach($age as $x=>$x_value)
  {
     echo "Key=" . $x . ", Value=" . $x_value;
     echo "
  ";
  }
  ?>
  输出:
 
  Key=Peter, Value=35
  Key=Ben, Value=37
  Key=Joe, Value=43
  4、使用ksort()
 
  ksort() 函数对关联数组按照键名进行升序排序。
 
  "35","Ben"=>"37","Joe"=>"43");
  ksort($age);
  foreach($age as $x=>$x_value)
  {
     echo "Key=" . $x . ", Value=" . $x_value;
     echo "
  ";
  }
  ?>
  输出:
 
  Key=Ben, Value=37
  Key=Joe, Value=43
  Key=Peter, Value=35
  5、使用arsort()
 
  arsort() 函数对关联数组按照键值进行降序排序。
 
  "35","Ben"=>"37","Joe"=>"43");
  arsort($age);
  foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "
  ";
  }
  ?>
  输出:
 
  Key=Joe, Value=43
  Key=Ben, Value=37
  Key=Peter, Value=35
  6、使用krsort()
 
  krsort() 函数对关联数组按照键名进行降序排序PHP数组排序。
 
  "35","Ben"=>"37","Joe"=>"43");
  krsort($age);
  foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "
  ";
  }
  ?>
  输出:
 
  Key=Peter, Value=35
  Key=Joe, Value=43
  Key=Ben, Value=37
 

(编辑:我爱资讯网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!