按数字键对数组进行排序

时间:2012-12-31 00:47:08

标签: php arrays array-key ksort

我想以数字方式对数组进行排序,即188,188-1,188-2,222,222-1,222,-2等。这就是数组当前的样子:

[188-1] => Array
    (
        [time] => 1
    )

[188-2] => Array
    (
        [time] => 2
    )

[188-3] => Array
    (
        [time] => 3
    )

[188] => Array
    (
        [notes] => frog stand notes
    )

[489] => Array
    (
        [notes] => notes
    )

[489-1] => Array
    (
        [weight] => 10
        [reps] => 30
    )

[489-2] => Array
    (
        [weight] => 20
        [reps] => 30
    )

[489-3] => Array
    (
        [weight] => 30
        [reps] => 30
    )

[492-1] => Array
    (
        [weight] => 500
        [distance] => 100000
    )

[492] => Array
    (
        [notes] => more notes
    )

我尝试了一个ksort,ksort($sorted, SORT_DESC);但是使用带连字符的密钥并不能很好地工作,除非我做错了什么?

1 个答案:

答案 0 :(得分:8)

您可以将uksort()strnatcmp()一起用作比较功能:

uksort($array, 'strnatcmp');