首页 > 解决方案 > 如何在 PHP 数组中进行分层排序?

问题描述

我想用分层键对表进行排序,但我不能:(这是一个例子:

array ( '1.1.1' => 'test1', '10.1.1' => 'test2', '2.1.1' => 'test3', 1 => 'test4', 2 => 'test5', 3 => 'test6', '0.1' => 'test7', 0 => 'test8', 10 => 'test9', )

结果必须是:

array ( '0' => 'test8', '0.1' => 'test7', '1' => 'test4', '1.1.1' => 'test1', '2' => 'test5', '2.1.1' => 'test3', '3' => 'test6',  '10' => 'test9', '10.1.1' => 'test2')

非常感谢 !

我的尝试: https ://notepad.pw/71vyf2f7

标签: php

解决方案


你看过uksort吗?http://php.net/manual/en/function.uksort.php

我认为这就是你需要的

$arr = array ( '1.1.1' => 'test1', '10.1.1' => 'test2', '2.1.1' => 'test3', 1 => 'test4', 2 => 'test5', 3 => 'test6', '0.1' => 'test7', 0 => 'test8', 10 => 'test9', );



function cmp($a,$b) {
       return floatval($a) > floatval($b);
}

uksort($arr, 'cmp');

print_r($arr)

推荐阅读