array sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

array sorting

Post by moiseszaragoza »

array sorting
I have a array that holds 2 values a userid and a score.

but i want to show the hiest scores on top.
but its not working. this is what i am trying

PHP Code:
$array_userid_score['score'][$count_userid_score] = 1;
$array_userid_score['users_id'][$count_userid_score] = 0;

$array_userid_score['score'][$count_userid_score] = 10;
$array_userid_score['users_id'][$count_userid_score] = 1;

$array_userid_score['score'][$count_userid_score] = 9;
$array_userid_score['users_id'][$count_userid_score] = 2;

$array_userid_score['score'][$count_userid_score] = 15;
$array_userid_score['users_id'][$count_userid_score] = 3;

$array_userid_score['score'][$count_userid_score] = 6;
$array_userid_score['users_id'][$count_userid_score] = 4;

asort ($array_userid_score);
But I am not getting it by the score

Thanks for the help
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: array sorting

Post by SimpleManWeb »

I believe you are going to want to use usort.

http://php.net/manual/en/function.usort.php

Look at the first example they have on the above link and that should get you headed in the right direction.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: array sorting

Post by McInfo »

Because there are actually two arrays ($array_userid_score['score'] and $array_userid_score['users_id']) and the score array needs to be sorted while keeping the users_id array in sync, array_multisort() is more appropriate.

However, as shown, this code does not make sense:

Code: Select all

$array_userid_score['score'][$count_userid_score]    =  1;
$array_userid_score['users_id'][$count_userid_score] =  0;
 
$array_userid_score['score'][$count_userid_score]    = 10;
$array_userid_score['users_id'][$count_userid_score] =  1;
 
$array_userid_score['score'][$count_userid_score]    =  9;
$array_userid_score['users_id'][$count_userid_score] =  2;
 
$array_userid_score['score'][$count_userid_score]    = 15;
$array_userid_score['users_id'][$count_userid_score] =  3;
 
$array_userid_score['score'][$count_userid_score]    =  6;
$array_userid_score['users_id'][$count_userid_score] =  4;
It doesn't make sense because $count_userid_score is not set; but more than that, if it is set, its value doesn't change; so the second pair of lines (4, 5) overwrites the score and users_id of the first pair of lines (1, 2) and the last pair (13, 14) overwrites all of the scores and users_ids that preceded it. That means there is no point in sorting because you are left with two arrays, each containing a single item.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 4:11 pm, edited 1 time in total.
harryroy
Forum Newbie
Posts: 3
Joined: Sat Jan 16, 2010 5:58 am

Re: array sorting

Post by harryroy »

Hi everybody
I want thanks to SimpleManWeb,He has given useful link for Php function reference collection website,This website have so many collection of Php function references with example,This website is really so useful for me to teach PHP ,
Thanks again SimpleManWeb
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: array sorting

Post by McInfo »

harryroy wrote:I want thanks to SimpleManWeb,He has given useful link for Php function reference collection website,This website have so many collection of Php function references with example,This website is really so useful for me to teach PHP ,
Those crazy manuals... they're always so darn useful.

Edit: This post was recovered from search engine cache.
Post Reply