在PHP中排序数组时出现问题

时间:2011-08-12 17:19:41

标签: php arrays sorting

我有一个像这样的PHP数组:

Array 
( 
    [0] => post9.post 
    [3] => post8.post 
    [4] => post4.post 
    [5] => post7.post 
    [6] => post5.post 
    [7] => post10.post 
    [8] => post3.post 
    [9] => post6.post 
    [10] => post1.post 
    [11] => post2.post 
)

但是在尝试了所有PHP数组函数后,我无法以这种方式对它进行排序:

Array 
( 
    [0] => post10.post 
    [1] => post9.post 
    [2] => post8.post 
    [3] => post7.post 
    [4] => post6.post 
    [5] => post5.post 
    [6] => post4.post 
    [7] => post3.post 
    [8] => post2.post 
    [9] => post1.post 
)

我已经提出了这样的问题,这似乎有效,但现在它不起作用:(

编辑:使用rsort后,它看起来像这样:

Array
(
    [0] => post9.post
    [1] => post8.post
    [2] => post7.post
    [3] => post6.post
    [4] => post5.post
    [5] => post4.post
    [6] => post3.post
    [7] => post2.post
    [8] => post10.post
    [9] => post1.post
)

几乎不错,除了post10.post应为[0][8]

1 个答案:

答案 0 :(得分:7)

您需要使用natsort然后撤消订单。

 natsort($array);
 $array = array_reverse($array);