php-usort()-数组排序

编程语言 / 218人浏览 / 0人评论

php-usort()-数组排序,使用用户自定义的比较函数对数组中的值进行排序,本函数将用用户自定义的比较函数对一个数组中的值进行排序。 如果要排序的数组需要用一种不寻常的标准进行排序,那么应该使用此函数。

php数组函数 usort()

支持版本

(PHP 4, PHP 5, PHP 7, PHP 8)

定义和用法

usort(array &$array, callable $callback): bool

usort — 使用用户自定义的比较函数对数组中的值进行排序

注意:如果两个成员完全相同,那么它们在排序数组中的相对顺序是未定义的。

注意: 此函数为 array 中的元素赋与新的键名。这将删除原有的键名,而不是仅仅将键名重新排序。

参数解释

参数描述
array输入的数组
callback在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。
callback(mixed $a, mixed $b): int

警告:Returning non-integer values from the comparison function, such as float, will result in an internal cast to int of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

函数返回值

成功时返回 true, 或者在失败时返回 false。

演示案例

<?php

namespace app\index\controller;

use app\common\controller\Base;

class Index extends Base
{
    public function cmp($a, $b)
    {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }

    public function cmps($a, $b)
    {
        return strcmp($a["fruit"], $b["fruit"]);
    }

    public function index()
    {        
    
        $a = array(3, 2, 5, 6, 1);
        //注意:很明显在这个小例子中用 sort() 函数更合适。
        usort($a, array('\app\index\controller\Index','cmp'));
        
        var_dump($a);

        foreach ($a as $key => $value) {
            echo "$key: $value\n";
        }
        // 排序值注意:区分大小写
        $fruits[]["fruit"] = "almond-杏仁";
        $fruits[]["fruit"] = "apple-苹果";
        $fruits[]["fruit"] = "apricot-杏子";
        $fruits[]["fruit"] = "arbutus-杨梅";
        $fruits[]["fruit"] = "avocado-南美梨";
        $fruits[]["fruit"] = "bennet-水杨梅";
        $fruits[]["fruit"] = "bagasse-甘蔗渣";
        $fruits[]["fruit"] = "berry-浆果";
        $fruits[]["fruit"] = "banana-香蕉";
        $fruits[]["fruit"] = "bergamot-佛手柑";
        $fruits[]["fruit"] = "betelnut-槟榔";
        $fruits[]["fruit"] = "bilberry-越桔";
        $fruits[]["fruit"] = "bitter  orange-苦酸橙";        
        $fruits[]["fruit"] = "blackberry-刺梅";
        $fruits[]['fruit'] = "blueberry-蓝莓";
        $fruits[]['fruit'] = "bryony-野葡萄";
        $fruits[]['fruit'] = "bullace-野李子";
        $fruits[]['fruit'] = "bush  fruit-丛生果";
        $fruits[]['fruit'] = "cantaloupe-美国甜瓜";
        $fruits[]['fruit'] = "carambola-杨桃";
        $fruits[]['fruit'] = "casaba-冬季甜瓜";
        $fruits[]['fruit'] = "cascara-鼠李";
        $fruits[]['fruit'] = "cherry-樱桃";
        $fruits[]['fruit'] = "chestnut-栗子";
        $fruits[]['fruit'] = "coconut-椰子";
        $fruits[]['fruit'] = "codlin-未熟苹果";
        $fruits[]['fruit'] = "core-果心";
        $fruits[]['fruit'] = "cranberry-蔓越橘";
        $fruits[]['fruit'] = "cumquat-金桔";
        $fruits[]['fruit'] = "custard  apple-番荔枝";
        $fruits[]['fruit'] = "damson-洋李子";
        $fruits[]['fruit'] = "date-枣子";
        $fruits[]['fruit'] = "date  palm-枣椰子";
        $fruits[]['fruit'] = "dew-果露";
        $fruits[]['fruit'] = "durian-榴莲";
        $fruits[]['fruit'] = "fig-无花果";
        $fruits[]['fruit'] = "flat  peach-蟠桃";
        $fruits[]['fruit'] = "foxnut-鸡头果";
        $fruits[]['fruit'] = "ginkgo-银杏";
        $fruits[]['fruit'] = "gooseberry-醋栗";
        $fruits[]['fruit'] = "grape-葡萄";
        $fruits[]['fruit'] = "grapefruit-葡萄柚";
        $fruits[]['fruit'] = "guava-番石榴";
        $fruits[]['fruit'] = "haw-山楂";
        $fruits[]['fruit'] = "herbaceous  fruit-草本果";
        $fruits[]['fruit'] = "kiwifruit['kiwi,frʊt]-奇异果,猕猴桃";
        $fruits[]['fruit'] = "lemon-柠檬";
        $fruits[]['fruit'] = "lichee[,lɪ'tʃi]-荔枝";
        $fruits[]['fruit'] = "longan ['lɔŋɡən]-龙眼、桂圆";
        $fruits[]['fruit'] = "loquat ['lokwɑt]-枇杷";
        $fruits[]['fruit'] = "lotus nut (seed)-莲子";
        $fruits[]['fruit'] = "mango 芒果";
        $fruits[]['fruit'] = "mandarin['mændərɪn]-中国柑桔";
        $fruits[]['fruit'] = "mangosteen['mæŋgə,stin]-山竹";
        $fruits[]['fruit'] = "muskmelon['mʌsk,mɛlən]-甜瓜总称";
        $fruits[]['fruit'] = "nectarine[ˌnɛktəˈrin]-油桃";
        $fruits[]['fruit'] = "olive-橄榄";
        $fruits[]['fruit'] = "orange-橙子";
        $fruits[]['fruit'] = "papaya [pə'paɪə] (pawpaw)-木瓜";
        $fruits[]['fruit'] = "peach-桃子";
        $fruits[]['fruit'] = "peanut-花生";
        $fruits[]['fruit'] = "pear-梨";
        $fruits[]['fruit'] = "persimmon[pɚ'sɪmən]-柿子";
        $fruits[]['fruit'] = "pineapple ['paɪn'æpl]-菠萝";
        $fruits[]['fruit'] = "pistachio[pɪˈstæʃiˌoʊ]-开心果";
        $fruits[]['fruit'] = "pitaya ['pɪtəjɚ]-火龙果";
        $fruits[]['fruit'] = "plum 李子";
        $fruits[]['fruit'] = "pomegranate ['pɑmɪɡrænɪt]-石榴";
        $fruits[]['fruit'] = "pomelo ['pɑməlo]-柚子";
        $fruits[]['fruit'] = "rambutan [ræm'butn]-红毛丹";
        $fruits[]['fruit'] = "raspberry ['ræzbəri]-树莓";
        $fruits[]['fruit'] = "shaddock ['ʃædək]-文旦";
        $fruits[]['fruit'] = "strawberry-草莓";
        $fruits[]['fruit'] = "sugar cane-甘蔗";
        $fruits[]['fruit'] = "sunflower seeds-瓜子";
        $fruits[]['fruit'] = "tangerine ['tændʒə'rin]-橘子";
        $fruits[]['fruit'] = "walnut['wɔlnət]-核桃";
        $fruits[]['fruit'] = "water caltrop['kæltrəp]-菱角";
        $fruits[]['fruit'] = "water-chestnut['tʃɛsnʌt]-马蹄";
        $fruits[]['fruit'] = "watermelon['wɔtɚmɛlən]-西瓜";
        $fruits[]['fruit'] = "waxberry['wæks,bɛri]-杨梅";
        $fruits[]['fruit'] = "wax apple-莲雾";
        $fruits[]['fruit'] = "pulp/flesh-果肉";
        $fruits[]['fruit'] = "core核";
        $fruits[]['fruit'] = "apple core-苹果核";
        $fruits[]['fruit'] = "pit核";
        $fruits[]['fruit'] = "apricot pit-杏仁核";
        $fruits[]['fruit'] = "kernel['kɝnl]-核";
        $fruits[]['fruit'] = "nucleus ['nuklɪəs]-核仁";
        $fruits[]['fruit'] = "seed-籽";
        $fruits[]['fruit'] = "skin-皮";
        $fruits[]['fruit'] = "peel-皮";
        $fruits[]['fruit'] = "tender-嫩的";
        $fruits[]['fruit'] = "bitter-苦的";
        $fruits[]['fruit'] = "canned fruit-水果罐头";
        $fruits[]['fruit'] = "crisp-脆的";
        $fruits[]['fruit'] = "rotten ['rɑtn]-烂的";
        $fruits[]['fruit'] = "ripe [raɪp]-熟的(特指水果)";
        $fruits[]['fruit'] = "unripe-未熟的";
        $fruits[]['fruit'] = "flavor-味道";
        $fruits[]['fruit'] = "fresh-新鲜的";
        $fruits[]['fruit'] = "hard-坚硬的";
        $fruits[]['fruit'] = "juice-果汁";
        $fruits[]['fruit'] = "juicy-多汁的";
        $fruits[]['fruit'] = "succulent-多汁的";
        $fruits[]['fruit'] = "dew-果露";
        $fruits[]['fruit'] = "seasonable-应季的";
        $fruits[]['fruit'] = "greengrocer ['ɡrinɡrosɚ]-蔬果商";
        //当排序多维数组时,$a 和 $b 包含到数组第一个索引的引用。
        usort($fruits, array('\app\index\controller\Index','cmps'));

        foreach($fruits as $key=>$value) {
            echo $key.':'.$value["fruit"] . "\n";
        }
    }
}

返回结果

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(5)
  [4]=>
  int(6)
}
0: 1
1: 2
2: 3
3: 5
4: 6
0:almond-杏仁
1:apple core-苹果核
2:apple-苹果
3:apricot pit-杏仁核
4:apricot-杏子
5:arbutus-杨梅
6:avocado-南美梨
7:bagasse-甘蔗渣
8:banana-香蕉
9:bennet-水杨梅
10:bergamot-佛手柑
11:berry-浆果
12:betelnut-槟榔
13:bilberry-越桔
14:bitter  orange-苦酸橙
15:bitter-苦的
16:blackberry-刺梅
17:blueberry-蓝莓
18:bryony-野葡萄
19:bullace-野李子
20:bush  fruit-丛生果
21:canned fruit-水果罐头
22:cantaloupe-美国甜瓜
23:carambola-杨桃
24:casaba-冬季甜瓜
25:cascara-鼠李
26:cherry-樱桃
27:chestnut-栗子
28:coconut-椰子
29:codlin-未熟苹果
30:core-果心
31:core核
32:cranberry-蔓越橘
33:crisp-脆的
34:cumquat-金桔
35:custard  apple-番荔枝
36:damson-洋李子
37:date  palm-枣椰子
38:date-枣子
39:dew-果露
40:dew-果露
41:durian-榴莲
42:fig-无花果
43:flat  peach-蟠桃
44:flavor-味道
45:foxnut-鸡头果
46:fresh-新鲜的
47:ginkgo-银杏
48:gooseberry-醋栗
49:grape-葡萄
50:grapefruit-葡萄柚
51:greengrocer ['ɡrinɡrosɚ]-蔬果商
52:guava-番石榴
53:hard-坚硬的
54:haw-山楂
55:herbaceous  fruit-草本果
56:juice-果汁
57:juicy-多汁的
58:kernel['kɝnl]-核
59:kiwifruit['kiwi,frʊt]-奇异果,猕猴桃
60:lemon-柠檬
61:lichee[,lɪ'tʃi]-荔枝
62:longan ['lɔŋɡən]-龙眼、桂圆
63:loquat ['lokwɑt]-枇杷
64:lotus nut (seed)-莲子
65:mandarin['mændərɪn]-中国柑桔
66:mango 芒果
67:mangosteen['mæŋgə,stin]-山竹
68:muskmelon['mʌsk,mɛlən]-甜瓜总称
69:nectarine[ˌnɛktəˈrin]-油桃
70:nucleus ['nuklɪəs]-核仁
71:olive-橄榄
72:orange-橙子
73:papaya [pə'paɪə] (pawpaw)-木瓜
74:peach-桃子
75:peanut-花生
76:pear-梨
77:peel-皮
78:persimmon[pɚ'sɪmən]-柿子
79:pineapple ['paɪn'æpl]-菠萝
80:pistachio[pɪˈstæʃiˌoʊ]-开心果
81:pitaya ['pɪtəjɚ]-火龙果
82:pit核
83:plum 李子
84:pomegranate ['pɑmɪɡrænɪt]-石榴
85:pomelo ['pɑməlo]-柚子
86:pulp/flesh-果肉
87:rambutan [ræm'butn]-红毛丹
88:raspberry ['ræzbəri]-树莓
89:ripe [raɪp]-熟的(特指水果)
90:rotten ['rɑtn]-烂的
91:seasonable-应季的
92:seed-籽
93:shaddock ['ʃædək]-文旦
94:skin-皮
95:strawberry-草莓
96:succulent-多汁的
97:sugar cane-甘蔗
98:sunflower seeds-瓜子
99:tangerine ['tændʒə'rin]-橘子
100:tender-嫩的
101:unripe-未熟的
102:walnut['wɔlnət]-核桃
103:water caltrop['kæltrəp]-菱角
104:water-chestnut['tʃɛsnʌt]-马蹄
105:watermelon['wɔtɚmɛlən]-西瓜
106:wax apple-莲雾
107:waxberry['wæks,bɛri]-杨梅

转载注明:

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我