Welcome to WebmasterWorld Guest from 54.161.64.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Sorting multidimensional array with another array

     
10:07 pm on Dec 11, 2011 (gmt 0)

5+ Year Member



I have an multidimensional array called $courses that looks as follows:

$courses['1']['COURSE_TITLE'] = 'Art';
$courses['1']['COURSE_ID'] = '23';
$courses['1']['DESCRIPTION'] = 'Art and design';

$courses['2']['COURSE_TITLE'] = 'English';
$courses['2']['COURSE_ID'] = '34';
$courses['2']['DESCRIPTION'] = 'English;

$courses['3']['COURSE_TITLE'] = 'Maths';
$courses['3']['COURSE_ID'] = '45';
$courses['3']['DESCRIPTION'] = 'Mathematics';

$courses['4']['COURSE_TITLE'] = 'Science';
$courses['4']['COURSE_ID'] = '67';
$courses['4']['DESCRIPTION'] = 'Science Description';

I want to be able to sort this array using another array like $sortcourses below. In this array I would like to define the order of the array based on the course_title, and if a subject is missing in the sort array then it just adds them in any order at the bottom

$sortcourses = array("Maths", "Science", "English");

Can anyone kindly help?

Many thanks
5:08 am on Dec 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



array_multisort($courses, $sortcourses);
var_dump($courses);

Something along the above lines (but not the exact lines) should work for you.
10:38 am on Dec 12, 2011 (gmt 0)

5+ Year Member



thanks for the reply, but the array_multisort doesnt seem to sort it at all. i've tried many different ways of array_multisort but none actually sort. I thought maybe it might be because the array is multidimensional and tried with less complex dimension but still doesnt work
1:04 pm on Dec 12, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I don't think you can use array_multisort() in this instance. AFAIK array_multisort() needs to sort the initial array by some simple means... SORT_REGULAR, SORT_NUMERIC or SORT_STRING. When a second array is passed, this second array gets sorted based on how the first array was sorted. Which is not what is required. The initial array needs to be sorted based on some arbitrary order, governed by another array.

However, usort() [uk3.php.net] can be made to work. usort() allows a comparison function to be passed as the second argument. Using the fact that $sortcourses is a numerically indexed array, we can use this to determine the correct sort order of the $courses array.

/** 
* 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);



Is there a better method?
1:24 pm on Dec 12, 2011 (gmt 0)

5+ Year Member



wow, thats awesome. it works perfectly. thanks penders
1:50 pm on Dec 13, 2011 (gmt 0)

5+ Year Member



Hi penders, I've just realised that the code only works if my $courses array has only one set values like below:

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
)

)


However, the problem I am facing is that my $courses array is dynamic and loops and creates multiple instances within the $courses array. but this function just stops the loop and it just sorts the first set of array. My array dynamically looks like this:

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
)

)



Array
(
[1] => Array
(
[COURSE_TITLE] => Maths
[COURSE_ID] => 5878
[DESCRIPTION] => 2011-64-5878-14384
)

[2] => Array
[COURSE_TITLE] => IT
[COURSE_ID] => 5849
[DESCRIPTION] => 2011-64-5849-14384
)

)


Any idea how I can sort this?
2:39 pm on Dec 13, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I'm not sure that I follow you? If your $courses array changes - new items added etc. then you will naturally need to sort it again by calling usort(), as above.
3:05 pm on Dec 13, 2011 (gmt 0)

5+ Year Member



Below the code you gave I have the following:

foreach($courses as $key => $val){
echo $courses[$key]['COURSE_TITLE'];
}


But the problem with that is that it only sorts the first set of array and then seems to skip the rest and a result my script doesnt loop.
3:33 pm on Dec 13, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...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


Is there an extra dimension to the array (although there doesn't appear to be from the code you have posted)? "Multiple instances within"? Repetitions of the data? Sorry, still not sure what you mean?

How is the $courses array dynamic? Where is the extra data coming from after the initial sort?
3:44 pm on Dec 13, 2011 (gmt 0)

5+ Year Member



The $courses array is dynamic as it brings up a list of courses per user/student which is echo'ed.

So for example, if i take out the function you gave and do print_r($courses), it then displays output like this:



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
)

)



But if I put your usort function, it works on the first student and then stops at the second student.
4:18 pm on Dec 13, 2011 (gmt 0)

5+ Year Member



Just an update. I got it to work. I performing the usort function at the wrong place. Because there is another function that creates the $courses array dynamically. I put the usort function straight after that loop and it now works perfectly. Thanks
4:46 pm on Dec 13, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Glad you got it sorted :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month