Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个数组,我需要先按“key_1”对它进行排序,然后(如果“key_1”中有多个类似的值)按“key_2”对它进行排序……我该怎么做,知道吗?
谢谢!
$my_array[0]['key_1'] = 2; $my_array[0]['key_2'] = 300; $my_array[1]['key_1'] = 2; $my_array[1]['key_2'] = 100; $my_array[2]['key_1'] = 1; $my_array[2]['key_2'] = 100;
这是一个简单的解决方案:
function my_sort($a, $b) { if ($a['key_1']==$b['key_1']) { if ($a['key_2']==$b['key_2']) { return 0; } else { return ($a['key_2']>$b['key_2']) ? 1 : -1; } } else { return ($a['key_1']>$b['key_1']) ? 1 : -1; } } usort($my_array, 'my_sort');