I am trying to sort by a column(s) in the following data structure that I have built like this:
$counter = 1;
$entity_list = array();
foreach($result as $rec){
$entity_list[$counter]['student_first_name'] = $rec->firstname;
$entity_list[$counter]['student_last_name'] = $rec->lastname;
$entity_list[$counter]['course_name'] = $rec->fullname;
.
.
$counter++;
}//end foreach
This is a var_dump of the $entity_list data structure.
array (size=150)
1 =>
array (size=3)
'student_first_name' => string 'Jane' (length=6)
'student_last_name' => string 'Smith' (length=7)
'course_name' => string 'Algebra 1A-MOD' (length=14)
2 =>
array (size=3)
'student_first_name' => string 'Fred' (length=6)
'student_last_name' => string 'Jones' (length=7)
'course_name' => string 'Algebra 1A-MOD' (length=14)
.
.
.
How do I use asort() or ksort() on this structure? I think i should be using ksort(), as it works on a key. I have tried ksort($entity_list,'student_last_name'), or asort($entity_list,'current_grade') for example.
Thank you.
You can use uasort
uasort($entity_list, 'mySort');
function mySort($a, $b) {
return ($a['student_last_name'] <==> $b['student_last_name']);
}
But if your data come from a DB, it would be a lot easier and lighter to ORDER BY.