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
array sorting
Moderator: General Moderators
- moiseszaragoza
- Forum Commoner
- Posts: 87
- Joined: Sun Oct 03, 2004 4:04 pm
- Location: Ft lauderdale
- Contact:
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: array sorting
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.
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
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:
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.
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;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.
Re: array sorting
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
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
Those crazy manuals... they're always so darn useful.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 ,
Edit: This post was recovered from search engine cache.