2012-12-31 123 views
2

我想對數組進行數值排序,即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);但它不帶連字符鍵工作也很好,除非我做錯了什麼?

+0

你可以看看:http://sourcefrog.n​​et/projects/natsort/ –

回答

8

您可以使用uksort()strnatcmp()作爲對比功能:

uksort($array, 'strnatcmp'); 
+0

工作了魅力。乾杯。 – ditto

+0

演示:http://codepad.org/8rrtecbK –