这个问题有点类似 Mysql 中 order by ,需要的就是在数组中模拟对不同字段的排序。
假如有以下数组:
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];
现在需要在数组中按照 chinese
顺序,假如有相同,就按 math
顺序,最后得到的应该是如下的数组:
$afterSort = [
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
];
请问大家有什么不同的方法可以实现?
这是我自己使用的版本,使用方法: