php - 如何在 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')
非常感谢 !
解决方案
你看过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)
推荐阅读
- amazon-web-services - Cloudwatch 警报拒绝访问
- c# - 从异步方法返回 IAsyncEnumerable
- reactjs - 如何将儿童类型传递给 HOC 反应的道具
- javascript - 将 MySQL 数据库集成/调用到前端 Web 应用程序的最佳方法是什么?
- python-3.x - 使用循环生成模式
- javascript - 搜索后打开 URL
- java - 假 API 自动化:断言不起作用
- azure - 部署然后访问包含 WCF 的我的 ASP.NET Web 应用程序,将返回“您无权查看此目录或页面”。
- r - 在 GLMMadaptive 包中生成效果图
- dask - 使用 Dask,张量流只能从安装了 2 个 GPU 的机器上检测到 1 个 GPU