homepage Welcome to WebmasterWorld Guest from 54.227.41.230
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Sorting multidimensional array with another array
ahmed24




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

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

 

Habtom




msg:4396886
 5:08 am on Dec 12, 2011 (gmt 0)

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

Something along the above lines (but not the exact lines) should work for you.

ahmed24




msg:4396993
 10:38 am on Dec 12, 2011 (gmt 0)

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

penders




msg:4397037
 1:04 pm on Dec 12, 2011 (gmt 0)

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?

ahmed24




msg:4397040
 1:24 pm on Dec 12, 2011 (gmt 0)

wow, thats awesome. it works perfectly. thanks penders

ahmed24




msg:4397400
 1:50 pm on Dec 13, 2011 (gmt 0)

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?

penders




msg:4397413
 2:39 pm on Dec 13, 2011 (gmt 0)

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.

ahmed24




msg:4397426
 3:05 pm on Dec 13, 2011 (gmt 0)

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.

penders




msg:4397436
 3:33 pm on Dec 13, 2011 (gmt 0)

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

ahmed24




msg:4397444
 3:44 pm on Dec 13, 2011 (gmt 0)

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.

ahmed24




msg:4397465
 4:18 pm on Dec 13, 2011 (gmt 0)

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

penders




msg:4397473
 4:46 pm on Dec 13, 2011 (gmt 0)

Glad you got it sorted :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved