PHP 数组排序

PHP数组函数

  • sort() - 对数组进行升序排列
  • rsort() - 对数组进行降序排列
  • asort() - 根据关联数组的值,对数组进行升序排列
  • ksort() - 根据关联数组的键,对数组进行升序排列
  • arsort() - 根据关联数组的值,对数组进行降序排列
  • krsort() - 根据关联数组的键,对数组进行降序排列

sort() 升序、rsort() 降序

字母实例

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
echo "\n";
rsort($cars);
?>

数字实例

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
echo "\n";
rsort($numbers);
?>

asort() - 值取升序,arsort() - 值取降序

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);

arsort($age);
?>

ksort() - 键取升序,krsort()键取降序

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);

krsort($age);
?>

  

posted on 2017-11-27 16:28  aisy绍  阅读(124)  评论(0编辑  收藏  举报

导航