shiyoushao

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);
?>

  

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2022-02-19
  • 2021-05-21
猜你喜欢
  • 2021-06-11
  • 2021-12-22
  • 2022-01-09
  • 2022-02-21
  • 2022-01-18
相关资源
相似解决方案