Forum Moderators: coopster
/**
* Comparsion function
* @return int 0 if $a == $b, -1 if $a < $b or +1 if $a > $b
*/
function cmpArr($a,$b) {
global $sortcourses;
// Lookup the values in the array that determines the sort order
// and return the numeric array index that we can use to determine the order
$aKey = array_search($a['COURSE_TITLE'],$sortcourses);
$bKey = array_search($b['COURSE_TITLE'],$sortcourses);
// If element not found in sort array then assume it should appear at the end
if ($aKey === false) {
return 1;
} elseif ($bKey === false) {
return -1;
}
// Both elements found in sort array - determine order
if ($aKey == $bKey) {
return 0;
}
return ($aKey < $bKey) ? -1 : 1;
}
/**
* Sort
*/
usort($courses, 'cmpArr');
print_r($courses);
...my $courses array is dynamic and loops and creates multiple instances within the $courses array.
...it only sorts the first set of array and then seems to skip the rest
Student Number: 1
Name:
Other information regarding student.
Array
(
[1] => Array
(
[COURSE_TITLE] => English
[COURSE_ID] => 5878
[DESCRIPTION] => 2011-64-5878-14384
)
[2] => Array
[COURSE_TITLE] => Science
[COURSE_ID] => 5849
[DESCRIPTION] => 2011-64-5849-14384
)
)
Student Number: 2
Name:
Other information regarding student.
Array
(
[1] => Array
(
[COURSE_TITLE] => Maths
[COURSE_ID] => 5878
[DESCRIPTION] => 2011-64-5878-14384
)
[2] => Array
[COURSE_TITLE] => Language
[COURSE_ID] => 5849
[DESCRIPTION] => 2011-64-5849-14384
)
)