Nodral Posted May 20, 2011 Share Posted May 20, 2011 Hi All I'm new to php and I have a multidimensional array set up which I need to sort for a results list from an assessment. The array is $gradout['name']['userid']=score. I need to sort the array by score in a decending order. ['name'] is a string and ['userid'] is a numeric. I've tried looking at the php manual but It's just confusing me. Can anyone explain to me in laymans terms what I need to do and why. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 Can you explain the array structure a little more? I would think that name and userid would be a one-to-one relationship, so it seems odd you would have multiple userid within a name key. But, take a look at this code and see if it does what you want. function ($a, $b) { if ($a['name'] == $b['name']) { return 0; } return ($a['name'] > $b['name']) ? 1 : -1; } usort('sortByScore', $gradout); Quote Link to comment Share on other sites More sharing options...
Nodral Posted May 20, 2011 Author Share Posted May 20, 2011 I don't really understand the function you've written. Where do I declare the $a and $b values in my code and how does it work? As I said, I'm a complete newbie and I'm the sort of person who like to know WHY things work so I can customize them for future use. The array is structured in that way as the next step is to output to a table which will have a column for username, a column for the score and then a column of radio buttons (with the return value for each as the userid). When the table is viewed, the users can select the radio button and this will then take them to a question by question breakdown of their score by requesting the information from a database where the user responses are held against userid. Is it possible to make the function generic so I can hold it in a library and call it into further scripts, as I get the feeling it may be very useful. Quote Link to comment Share on other sites More sharing options...
fugix Posted May 20, 2011 Share Posted May 20, 2011 addressing the first question...you would declare $a and $b when you call the function e.g function ($var_a , $var_b) Quote Link to comment Share on other sites More sharing options...
Nodral Posted May 20, 2011 Author Share Posted May 20, 2011 So in the example given, how do I call the function? and what variables do I put in as $a and $b I'm assuming that you forgot to put a function name in, which would be sortbyscore? So I would call sortbyscore($gradout, ??????) Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 Did you even TRY the code I provided? As for "I'm the sort of person who like to know WHY things work so I can customize them for future use." There is a manual for PHP you know. That is how I learned PHP. I looked at others' code and walked thorough it line-by-line to see what they were doing and where. If I ran into a line I didn't understand I would look at the manual for the function, look at the examples, etc. until I did understand the line of code. You don't need to declare anything. The $a and $b are variables declared as part of the function. The function is called from within the usort() function. The usort() function will pass every combination of two values from the array to the function as $a and $b then, based upon the result of the function will sort the two values accordingly. Take a look at the manual for usort(). THEN, if you have any questions you can ask them here. http://us.php.net/manual/en/function.usort.php Run this function ($a, $b) { if ($a['name'] == $b['name']) { return 0; } return ($a['name'] > $b['name']) ? 1 : -1; } echo "<pre>Before sorting:\n"; print_r($gradout); usort('sortByScore', $gradout); echo "\n\nAfter sorting:\n"; print_r($gradout); echo "<pre>"; Quote Link to comment 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.