Jump to content

Help with Array_multisort


djanim8

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/80081-help-with-array_multisort/
Share on other sites

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');

?>

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)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.