/    Sign up×
Community /Pin to ProfileBookmark

arrays & ranks

i have users, and they each have a number, the higher the number the better, i want to give the users ranks, 1 being the highest.
I tried

[CODE]
$array = array(‘8′, ’65’, ‘2’, ’45’);
rsort($array);
while (list($key, $val) = each($array)) {
$key2 = $key + 1;
echo ‘Rank ‘ . $key2 . ‘: ‘ . $val . “APn”;
}
[/CODE]

that puts the numbers in the right order and gives a rank, but i have no way of matching these results to the username. How can this be achieved?
Can i do something like this:

[CODE]
$array = array(‘usera’ => ’65’, ‘userb’ => ‘6’, ‘userc’ => ’23’, ‘userd’ => ‘4’);
rsort($array);
while (list($key, $val) = each($array)) {
$key2 = $key + 1;
echo ‘Rank ‘ . $key2 . ‘: ‘ . $val . “APn”;
}
[/CODE]

but then i would need to get the key value into the loop, but it’s already in the loop as a number. Is there a better way, or a way that works?

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 02.2005 — I [i]think[/i] this is what you are after:
[code=php]
<?php
$array = array('usera' => '65',
'userb' => '6',
'userc' => '23',
'userd' => '4');
rsort($array);
foreach($array as $key=>$value) {
echo "Rank $key: {$value}APn";
}
?>
[/code]
×

Success!

Help @Scleppel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...