Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-21-2006, 11:45 AM Ordering an array
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Hi all,

The answer to this question might be really simple but my thinking process is so clogged up that i can't think of the answer.

Let's say there are 5 students in an exam and their results are stored in array like this:

$student[] = array("John", "59");
$student[] = array("Mary", "42");
$student[] = array("Steve", "72");
$student[] = array("Harry", "31");
$student[] = array("Sally", "59");

How can this same array be re-ordered according to the exam result in descending order?

All of your help is greatly appreciated.
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
 
Register now for full access!
Old 07-21-2006, 12:03 PM Re: Ordering an array
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Why store it like that?

The best way would be storing by unique names, since their names are all unique in your example:

PHP Code:
$students = array('John' => 59'Mary' => 42'Steve' => 72'Harry' => 31'Sally' => 59); 
this is the same as doing:

PHP Code:
$students['John'] = 59;
$students['Mary'] = 42;
... 
Then to sort, do

PHP Code:
arsort($students); 
And that's it.

If you do want to keep it as a multidimensional array, then (rename your variable to $students[] it makes more sense this way)

PHP Code:
foreach($students as $key => $row)
{
  
$student[$key] = $row[0];
  
$results[$key] = $row[1];
}
array_multisort($resultsSORT_DESC$studentSORT_ASC$students); 
Basically the way we look at the information is important for working with multidimensional arrays, we have our main array, which holds all the students, it is built up of smaller arrays which contains the student and results hence my variable naming.

We first need to go through our array and store the information inside it in their own array, so storing the same information in it's own array, this way when sorting, we are still working with the same information (3 times as much), but that's what you asked for and this is the only way I know how we can do it.

It is quite confusing, but knowing that $row[0] is where our students are and $row[1] is where the results are (could have called it $col which may make it clearer for you, I just worked on what PHP output in print_r), we want to sort the results first, then students (if they have the same results, then we'd like the person with the lower alpha character to have higher precedance), then we tell it the main array so that it can update it to reflect the changes for the keys.

Before:
Code:
Array
(
    [0] => Array
        (
            [0] => John
            [1] => 59
        )

    [1] => Array
        (
            [0] => Mary
            [1] => 42
        )

    [2] => Array
        (
            [0] => Steve
            [1] => 72
        )

    [3] => Array
        (
            [0] => Harry
            [1] => 31
        )

    [4] => Array
        (
            [0] => Sally
            [1] => 59
        )

)
After:
Code:
Array
(
    [0] => Array
        (
            [0] => Steve
            [1] => 72
        )

    [1] => Array
        (
            [0] => John
            [1] => 59
        )

    [2] => Array
        (
            [0] => Sally
            [1] => 59
        )

    [3] => Array
        (
            [0] => Mary
            [1] => 42
        )

    [4] => Array
        (
            [0] => Harry
            [1] => 31
        )

)
Hopefully what I suggest helps you.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers; 07-21-2006 at 12:49 PM..
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 07-21-2006, 01:46 PM Re: Ordering an array
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
MC,

That has to be the best answer i've ever received on any forum.

Thanks!
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 07-22-2006, 12:27 AM Re: Ordering an array
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
One question though, to better-help me understand this. Would

PHP Code:
foreach($students as $key => $row)
{
  
$student[$key] = $row[0];
  
$results[$key] = $row[1];
}
array_multisort($resultsSORT_DESC$studentSORT_ASC$students); 
Be the same as:
PHP Code:
foreach($students as $key => $row)
{
  
$student[] = $row[0];
  
$results[] = $row[1];
}
array_multisort($resultsSORT_DESC$studentSORT_ASC$students); 
I.e. Does [$key] need to be there or can it be replaced with []?
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 07-22-2006, 12:42 AM Re: Ordering an array
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Yes - It appears so because the array keys are numaric starting at 0. If you wanted to perserve the keys, like if they were numeric or string representing a specific value, then that would be best. Looking at mastercomputers array sample above, that is not the case, the keys are numeric starting from 0.

So, to make it even more simple, you could bypass the keys...

foreach($students as $row) { . . .
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-22-2006, 06:42 AM Re: Ordering an array
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Read manual about usort(), it will allow you to sort any arrays of any types of data
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 07-22-2006, 09:56 AM Re: Ordering an array
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Thanks for the help guys.
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 07-22-2006, 11:18 AM Re: Ordering an array
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Hey fambi,

You are correct that student[] is the same in this example, however you must realise that this actually means it will add to the end of an array, if it's just first time creating an array (like this example), then it will always start at zero. You can think if $key as a counter in this, starting from 0 and incrementing in each loop.

e.g. if I did this when creating an array

PHP Code:
$student[5] = 'John';
$student[] = 'Peter'
In this example, $student[6] == 'Peter'; Even if we've skipped 0-4. So you can have mixed results if you are specifically wanting to overwrite the array but if wanting to append to it, then use your method, it's quite safe doing it this way too, if you don't want results overwritten as you can always sort it afterwards and remove double ups, etc.

Code what you're comfortable with is what I recommend, but you can pick up bad habits if you're not careful. I would have recommended that numbers not be quoted as computers handle numbers better than strings, but that's just a performance thing really as PHP's type handling will probably treat it as a number and string so no need to worry about it's type, in C/C++ however, it's a string.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers; 07-22-2006 at 11:21 AM..
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Ordering an array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28779 seconds with 12 queries