php数组排序

php关于数组排序的内置函数太多了,大部分都很少用,不过有些场景真的就一个函数就搞定了,特此记录!

一、对数组值进行排序

sort

  1. 语法 sort(array &$array[, int $sort_flags = SORT_REGULAR] ):bool
  2. 说明:对数组的值进行升序排序
    • 引用传值方式
    • 如果是字母则按字母顺序排序
    • 如果同时包含数字、字母,则字母靠前数字靠后
    • 排序后变为索引数组
    • 二维数组则根据数组元素个数升序排序,下标从非二维数组下标开始递增,且二维数组内部顺序不变
  3. 可选参数$sort_flags

    • SORT_REGULAR - 正常比较单元(不改变类型)
    • SORT_NUMERIC - 单元被作为数字来比较
    • SORT_STRING - 单元被作为字符串来比较
    • SORT_LOCALE_STRING - 根据当前的区域(locale)设置来把单元当作字符串比较,可以用 setlocale() 来改变。
    • SORT_NATURAL - 和 natsort() 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。
    • SORT_FLAG_CASE - 能够与 SORT_STRING 或 SORT_NATURAL 合并(OR 位运算),不区分大小写排序字符串。
  4. demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    例一:
    $arr = [2, 4, 56, 9, 134];
    sort($arr);
    print_r($arr);

    sort($arr, SORT_STRING);
    print_r($arr);

    输出结果:
    Array
    (
    [0] => 2
    [1] => 4
    [2] => 9
    [3] => 56
    [4] => 134
    )

    Array
    (
    [0] => 134
    [1] => 2
    [2] => 4
    [3] => 56
    [4] => 9
    )

    -----------------------------------------------------------

    例二:
    $arr = [
    'a' =>'z',
    'two'=> 'a',
    5,3,1,2,
    'three'=> 'd',
    'four' => [
    'aaa'=>'dddd',
    'ccc'=> 'aaa'
    ],
    'zzz' => [
    'zzz',
    'aaa',3,1,2]
    ];
    print_r($arr);
    sort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [a] => z
    [two] => a
    [0] => 5
    [1] => 3
    [2] => 1
    [3] => 2
    [three] => d
    [four] => Array
    (
    [aaa] => dddd
    [ccc] => aaa
    )

    [zzz] => Array
    (
    [0] => zzz
    [1] => aaa
    [2] => 3
    [3] => 1
    [4] => 2
    )

    )

    排序后:
    Array
    (
    [0] => a
    [1] => d
    [2] => z
    [3] => 1
    [4] => 2
    [5] => 3
    [6] => 5
    [7] => Array
    (
    [aaa] => dddd
    [ccc] => aaa
    )

    [8] => Array
    (
    [0] => zzz
    [1] => aaa
    [2] => 3
    [3] => 1
    [4] => 2
    )

    )

rsort

  1. 语法rsort(array &$array[, int $sort_flags = SORT_REGULAR]):bool
  2. 说明:对数组的值进行降序排序
    • 如果是字母则按字母逆序排序
    • 如果同时包含数字、字母,则数字靠前字母靠后
    • 排序后变为索引数组
    • 二维数组则根据数组元素个数降序排序,下标从0开始,且二维数组内部顺序不变
  3. 可选参数$sort_flags
    • SORT_REGULAR - 正常比较单元(不改变类型)
    • SORT_NUMERIC - 单元被作为数字来比较
    • SORT_STRING - 单元被作为字符串来比较
    • SORT_LOCALE_STRING - 根据当前的区域(locale)设置来把单元当作字符串比较,可以用 setlocale() 来改变。
    • SORT_NATURAL - 和 natsort() 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。
    • SORT_FLAG_CASE - 能够与 SORT_STRING 或 SORT_NATURAL 合并(OR 位运算),不区分大小写排序字符串。
  4. demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    例一:
    $arr = [2,4,56,9,134];
    rsort($arr);
    print_r($arr);

    rsort($arr, SORT_STRING);
    print_r($arr);

    输出:
    Array
    (
    [0] => 134
    [1] => 56
    [2] => 9
    [3] => 4
    [4] => 2
    )
    Array
    (
    [0] => 9
    [1] => 56
    [2] => 4
    [3] => 2
    [4] => 134
    )

    -----------------------------------------------------------

    例二:
    $arr = [
    'a' =>'z',
    'two'=> 'a',
    5,3,1,2,
    'three'=> 'd',
    'four' => [
    'aaa'=>'dddd',
    'ccc'=> 'aaa'
    ],
    'zzz' => [
    'zzz',
    'aaa',3,1,2]
    ];
    print_r($arr);
    rsort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [a] => z
    [two] => a
    [0] => 5
    [1] => 3
    [2] => 1
    [3] => 2
    [three] => d
    [four] => Array
    (
    [aaa] => dddd
    [ccc] => aaa
    )

    [zzz] => Array
    (
    [0] => zzz
    [1] => aaa
    [2] => 3
    [3] => 1
    [4] => 2
    )

    )
    排序后:
    Array
    (
    [0] => Array
    (
    [0] => zzz
    [1] => aaa
    [2] => 3
    [3] => 1
    [4] => 2
    )

    [1] => Array
    (
    [aaa] => dddd
    [ccc] => aaa
    )

    [2] => 5
    [3] => 3
    [4] => 2
    [5] => 1
    [6] => z
    [7] => d
    [8] => a
    )

真题:给定一个数组,如$arr = [2,4,56,9,134];连接所有元素,取最大值,如当前数组最大值为:95642134。

asort

  1. 语法asort(array &$array[, int $sort_flags = SORT_REGULAR]):bool
  2. 说明:对数组进行排序,保持原有的索引关系
    • 特性和sort一致
  3. demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    $arr = array(
    "d" => "lemon",
    5,1,4,2,3,
    "a" => "orange",
    "b" => "banana",
    "c" => "apple",
    'zzz' => array('111'),
    'aaa' => array('aaa','bbb')
    );
    print_r($arr);
    asort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [d] => lemon
    [0] => 5
    [1] => 1
    [2] => 4
    [3] => 2
    [4] => 3
    [a] => orange
    [b] => banana
    [c] => apple
    [zzz] => Array
    (
    [0] => 111
    )

    [aaa] => Array
    (
    [0] => aaa
    [1] => bbb
    )

    )
    排序后:
    Array
    (
    [c] => apple
    [b] => banana
    [d] => lemon
    [a] => orange
    [1] => 1
    [3] => 2
    [4] => 3
    [2] => 4
    [0] => 5
    [zzz] => Array
    (
    [0] => 111
    )

    [aaa] => Array
    (
    [0] => aaa
    [1] => bbb
    )

    )

arsort

  1. 语法arsort(array &$array[, int $sort_flags = SORT_REGULAR]):bool
  2. 说明:对数组进行逆向排序并保持索引关系
    • 特性和rsort一致
  3. demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    $arr = array(
    "d" => "lemon",
    5,1,4,2,3,
    "a" => "orange",
    "b" => "banana",
    "c" => "apple",
    'zzz' => array('111'),
    'aaa' => array('aaa','bbb')
    );
    print_r($arr);
    arsort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [d] => lemon
    [0] => 5
    [1] => 1
    [2] => 4
    [3] => 2
    [4] => 3
    [a] => orange
    [b] => banana
    [c] => apple
    [zzz] => Array
    (
    [0] => 111
    )

    [aaa] => Array
    (
    [0] => aaa
    [1] => bbb
    )

    )
    排序后:
    Array
    (
    [aaa] => Array
    (
    [0] => aaa
    [1] => bbb
    )

    [zzz] => Array
    (
    [0] => 111
    )

    [0] => 5
    [2] => 4
    [4] => 3
    [3] => 2
    [1] => 1
    [a] => orange
    [d] => lemon
    [b] => banana
    [c] => apple
    )

二、对数组键进行排序

ksort(array &$array[, int $sort_flags = SORT_REGULAR]):bool

  1. 对数组按照键名升序排序
    • 若键名同时包含大小写字母和数字,则顺序依次为[A-Z]a0[b-z][1-n]【特殊记忆a0的顺序关系】
    • 主要用于关联数组排序,若同时有关联和数字索引,注意顺序
  2. demo
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    $arr = array(
    8,1,2,6,4,
    'z' => 'z',
    'a' => 'a',
    'A' => 'A',
    'Z' => 'Z',
    'd' => 'd',
    'b' => 'b',
    '3' => array(3,1,2),// 覆盖了6
    'f' => array(3),
    );
    print_r($arr);
    ksort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [0] => 8
    [1] => 1
    [2] => 2
    [3] => Array
    (
    [0] => 3
    [1] => 1
    [2] => 2
    )

    [4] => 4
    [z] => z
    [a] => a
    [A] => A
    [Z] => Z
    [d] => d
    [b] => b
    [f] => Array
    (
    [0] => 3
    )

    )
    排序后:
    Array
    (
    [A] => A
    [Z] => Z
    [a] => a
    [0] => 8
    [b] => b
    [d] => d
    [f] => Array
    (
    [0] => 3
    )

    [z] => z
    [1] => 1
    [2] => 2
    [3] => Array
    (
    [0] => 3
    [1] => 1
    [2] => 2
    )

    [4] => 4
    )

krsort(array &$array[, int $sort_flags = SORT_REGULAR]):bool

  1. 对数组按照键名降序排序,保留原有的键值关系
    • 若键名同时包含大小写字母和数字,则顺序依次为[n-0][z-a][A-Z]【和ksort不太一致?】
    • 主要用于关联数组排序,若同时有关联和数字索引,注意顺序
  2. demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    $arr = array(
    8,1,2,6,4,
    'z' => 'z',
    'a' => 'a',
    'A' => 'A',
    'Z' => 'Z',
    'd' => 'd',
    'b' => 'b',
    '3' => array(3,1,2),
    'f' => array(3),
    );
    print_r($arr);
    krsort($arr);
    print_r($arr);

    原数组:
    Array
    (
    [0] => 8
    [1] => 1
    [2] => 2
    [3] => Array
    (
    [0] => 3
    [1] => 1
    [2] => 2
    )

    [4] => 4
    [z] => z
    [a] => a
    [A] => A
    [Z] => Z
    [d] => d
    [b] => b
    [f] => Array
    (
    [0] => 3
    )

    )
    排序后:
    Array
    (
    [4] => 4
    [3] => Array
    (
    [0] => 3
    [1] => 1
    [2] => 2
    )

    [2] => 2
    [1] => 1
    [0] => 8
    [z] => z
    [f] => Array
    (
    [0] => 3
    )

    [d] => d
    [b] => b
    [a] => a
    [Z] => Z
    [A] => A
    )

用户自定义函数排序

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

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

uasort(array &$array, callable $value_compare_func):bool

  1. 使用用户自定义的比较函数对数组中的值进行排序并保持原有索引关系

uksort(array &$array, callable $key_compare_func):bool

  1. 使用用户自定义的比较函数对数组中的键名进行排序

自然顺序排序

natsort(array &$array):bool

  1. 用“自然排序”算法对数组排序

natcasesort(array &$array):bool

  1. 用“自然排序”算法对数组进行不区分大小写字母的排序