简体   繁体   中英

Sorting an Array by key, numerically

I want to sort the arrays numerically, ie 188, 188-1, 188-2, 222, 222-1, 222,-2 etc. This is how the array currently looks:

[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
    )

I've tried a ksort, ksort($sorted, SORT_DESC); but it doesn't work too well with hyphenated keys, unless I'm doing something wrong?

您可以使用uksort()strnatcmp()作为比较函数:

uksort($array, 'strnatcmp');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM