djanim8 Posted December 4, 2007 Share Posted December 4, 2007 OK I don't know if I'm tired or what, but I can't figure out this array multisort thing.. I have a bunch of records with songs that were requested, selected by DISTINCT and then getting the number of records of each song, then inputing this data: $requestArray[$arNum] = array($songCount, $countRow['title'], $countRow['artist'], $countRow['disc'], $countRow['track'], $countRow['genre']); Then I want to sort it by the genre, and the songCount like this: array_multisort($requestArray[5], SORT_ASC, SORT_STRING, $requestArray[0], SORT_NUMERIC, SORT_DESC); And it won't sort... so what am I missing, what am I doing wrong? btw.. for some reason it it does this on the very first row: Genre_____________________Artist__Song__Disc____________________Track_______# Reqs Country - 2000 - Vol. 14______17_____6____Cross Canadian Ragweed___Country______ when it should look like this: Genre______Artist__________________Song__Disc___________________Track____# Reqs Country____Cross Canadian Ragweed___17___Country - 2000 - Vol. 14___6________1 and I have NO idea why, it only does it on the sort... Quote Link to comment https://forums.phpfreaks.com/topic/80081-help-with-array_multisort/ Share on other sites More sharing options...
Barand Posted December 4, 2007 Share Posted December 4, 2007 I prefer custom sort functions for sorting 2D arrays <?php function mysort ($a, $b) { $sc = strcmp ($a[5], $b[5]); // compare strings return ($sc==0) ? $b[0] - $a[0] : $sc; // if strings same, sort by requests } usort ($requestArray, 'mysort'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/80081-help-with-array_multisort/#findComment-405901 Share on other sites More sharing options...
djanim8 Posted December 4, 2007 Author Share Posted December 4, 2007 will this sort the whole array? I don't completley understand what was going on in that function LOL first variable is the array ($a) and the second is what to sort on? I need it to sort on 2 "colums" in the array. Quote Link to comment https://forums.phpfreaks.com/topic/80081-help-with-array_multisort/#findComment-406088 Share on other sites More sharing options...
Barand Posted December 5, 2007 Share Posted December 5, 2007 will this sort the whole array? Yes. I don't completley understand what was going on in that function LOL usort passes each pair of array elements for comparison ($a and $b) to the function. If $a should sort before $b, returns a number < 0. If a should sort below b, returns a number > 0; If they are equal, return 0. (www.php.net/usort) Quote Link to comment https://forums.phpfreaks.com/topic/80081-help-with-array_multisort/#findComment-407200 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.