lilingjie2015 发表于 2017-3-25 08:06:06

php 自定义排序

<?php

$a =array( 0 => Array
                (
                  "name" => 1,
                  "id" => 1,
                  "rank" => 1,
                ),

            1 => Array
                (
                  "name" => 2,
                  "id" => 2,
                  "rank" => 6,
                ),

            2 => Array
                (
                  "name" => 3,
                  "id" => 3,
                  "rank" => 3,
                ),

      );
      
      
function cmp($a, $b)
{
   if ($a['rank'] == $b['rank']) {
      return 0;
    }
return ($a['rank'] < $b['rank']) ? -1 : 1;
}


usort($a, "cmp");
print_r($a);

/*
注:如果自定义排序方法是写在 类里面的调用方式如下
usort($lines, array("AutoModelsEditAction","selfSort")); // AutoModelsEditAction 为排序所在类名,selfSort 为自定义排序方法
*/


?>
页: [1]
查看完整版本: php 自定义排序