I have the following two arrays:
array1
[0] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24 [reg] => 1
)
[1] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
[reg] => 2
)
array2
[0] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24
)
[1] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24
)
[2] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
)
[3] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
)
I have merged these two arrays using array_merge and the new array is array3
I have sorted array3 using the following function:
function cmp($a, $b) {
return strnatcasecmp( $a[course_id], $b[course_id] );
}
usort($array3, "cmp");
However the results I am looking to get do not show as I want below, where the course_id=1 from array 1 is always at the top, followed by a list of matching Course_ids from array two, then course_id = 2 from array 1, followed by a list of matching Course_ids from array 2. :
[0] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24
[reg] => 1
)
[1] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24
)
[2] => Array
(
[date] => 2014-02-01
[course_id] => 1
[id] => 24
)
[3] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
[reg] => 2
)
[4] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
)
[5] => Array
(
[date] => 2014-02-01
[course_id] => 2
[id] => 25
)
The function that I have used to sort array 3 doesnt all the time provide the desired results. Please could you help with this query.
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.