Jump to content

Sorting a Multidimensional Array


Nodral

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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, ??????)

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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