zoukankan      html  css  js  c++  java
  • PHP 数组排序

    PHP - 数组排序函数

    在本章中,我们将一一介绍下列 PHP 数组排序函数:

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

    1、sort() - 对数组进行升序排列

    <?php
    $cars=array("Volvo","BMW","Toyota");
    sort($cars);
    print_r($cars);
    ?>

    2、rsort() - 对数组进行降序排列

    下面的实例将 $cars 数组中的元素按照字母降序排列:

    实例

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

    3、asort() - 根据数组的值,对数组进行升序排列

    下面的实例根据数组的值,对关联数组进行升序排列:

    实例

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

    4、krsort() - 根据数组的键,对数组进行降序排列

    下面的实例根据数组的键,对关联数组进行降序排列:

    实例

    <?php
    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
    krsort($age);
    ?>
  • 相关阅读:
    超神头文件
    世界上还有比二分更容易错的算法吗?
    【POJ 1734】Sightseeing trip
    P1303 A*B Problem
    P1601 A+B Problem(高精)
    P1051 谁拿了最多奖学金
    【P1025】数的划分
    P1005 矩阵取数游戏
    P1006 传纸条
    邮票问题
  • 原文地址:https://www.cnblogs.com/duanjialin007/p/9648497.html
Copyright © 2011-2022 走看看