如何根据字符串对数组进行排序? 以下代码无法通过guardian_last_name对数组进行排序。我从之前的答案中得到了以下代码,用字符串排序。这个问题已被多次询问,但下面的代码应该有效。
我按姓氏排序,你可以看到它没有按姓氏排序。结果数组如下。我的问题很简单。如何按照守护者姓氏的字母顺序对多维数组进行排序?
usort($data, 'sortByOption');
function sortByOption($a, $b) {
return strcmp($a['guardian_last_name'], $b['guardian_last_name']);
}
(int) 0 => [
'guardian_first_name' => 'amy',
'guardian_last_name' => 'mael',
'guardian_id' => (int) 482
],
(int) 1 => [
'guardian_first_name' => 'pen',
'guardian_last_name' => 'kuehn',
'guardian_id' => (int) 244
],
(int) 2 => [
'guardian_first_name' => 'amy',
'guardian_last_name' => 'mael',
'guardian_id' => (int) 54