Page 1 of 1

array sorting

Posted: Sun Jan 17, 2010 11:47 am
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

Re: array sorting

Posted: Sun Jan 17, 2010 12:03 pm
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.

Re: array sorting

Posted: Sun Jan 17, 2010 9:54 pm
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.

Re: array sorting

Posted: Mon Jan 18, 2010 6:18 am
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

Re: array sorting

Posted: Mon Jan 18, 2010 11:01 am
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.