By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Ranking elements of an array

P: n/a
I have an array like

$a = array('US'=>50, 'CA'=>20, 'GB'=>90, 'MX'=>30);.

What I want is a rank of the keys according to their corresponding
values. For instance, since 'GB' is 90, GB is the first. In this
example, the result I want is an array

$r = array('GB', 'US', 'MX', 'CA');

Are there any functions that I can use to get this without having to
write "for loop"s?
Aug 22 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Aug 22, 5:20*pm, vijay <vijay...@gmail.comwrote:
I have an array like

$a = array('US'=>50, 'CA'=>20, 'GB'=>90, 'MX'=>30);.

What I want is a rank of the keys according to their corresponding
values. For instance, since 'GB' is 90, GB is the first. In this
example, the result I want is an array

$r = array('GB', 'US', 'MX', 'CA');

Are there any functions that I can use to get this without having to
write "for loop"s?
http://uk.php.net/manual/en/function.asort.php
Aug 22 '08 #2

P: n/a
..oO(vijay)
>I have an array like

$a = array('US'=>50, 'CA'=>20, 'GB'=>90, 'MX'=>30);.

What I want is a rank of the keys according to their corresponding
values. For instance, since 'GB' is 90, GB is the first. In this
example, the result I want is an array

$r = array('GB', 'US', 'MX', 'CA');

Are there any functions that I can use to get this without having to
write "for loop"s?
<?php
$a = array('US'=>50, 'CA'=>20, 'GB'=>90, 'MX'=>30);
arsort($a);
$r = array_keys($a);
?>

Micha
Aug 22 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.