Hey how ya's all doing? good? good, anywho, I've got a bit of a problem sorting an array and I was hoping someone out there might be able to help :}

Basically, sorting with mysql has made me lazy, and now I need to sort an array without mysql's funkeh sort stuff I'm stumped :/

What it is, is I have a site where someone can search a sort of FAQ database for answers to a question they might have, and then it stores the results in a 2d array, like this..

$resultarray[0][0]= the id field of the database entry which has matches to their keywords &
$resultarray[0][1]= the number of matches of their keywords in that database entry

Now that all works, but what I want then is to sort that array by the number of matches (the second part of the array) so the highest number of matches will be at the top of the list.

I've scoured every array tutorial I can find and tried to make sense of the php manual sort functions but I can't get it to work =[ Anyone got any ideas? I got a feeling I'm just missing something real simple but I can't work out what 🙁

    counting the number of keys in the array?

    Maybe I understood the problem incorrectly....

      umm not too sure what you mean, can't seem to get my head round this one at all 🙂

      this is the code that writes the details to the array...

      if($matches>0){
      $iv=$stuff[0]; // database entry's ID number
      $resultarray[$c][0]=intval($matches);
      $resultarray[$c][1]=intval($iv);  
      }

      ($c is just a counter which loops for the total matching queries in the database)

      so for example, if entry ID 1 in the database had 4 matches, and entry ID 2 has 3, the array would look like this

      matches | database id number
      --------------------------------------
          4               1
          3               2
      

      so I want to sort the array by the 'matches' column

        Check out the usort() function. Take it home. Get it drunk...

        usort(): More fun than a Schwartzian Transform!

          Write a Reply...