473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ranking elements of an array

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
2 3859
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Stuart Wexler | last post by:
Hi, I want to set up a JS/DHTML page that allows someone to graphically/manually rank items. For instance, they would have 10 movies, and they can move things around, 1 to 10, according to...
11
by: Petre Huile | last post by:
I have designed a site for a client, but they have hired an internet marketing person to incrase their search engine ranking and traffic. He wants to put extra-large fonts on every page which will...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
1
by: Joseph Bloch | last post by:
In all the threads on ranking in Access queries, I've not seen anything that deals with ranking against a subset of the query results. What I need to do is take the following query results: Dept...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
5
by: Chris | last post by:
I was wodering if there was a way to rank numbers in a query like this #'s Rank 100 1 99 2 98 3 98 97 5 96 6 96
3
by: katak | last post by:
hi... 1. how to do ranking value in array ? let said i hv 200000 sets of data need to store in an array after that i need to do ranking for the 200000 sets of data and find out the top 20 sets...
0
by: Maciej Gawinecki | last post by:
Hello, Recently I've interested in quering XML document streams. They can be queried in keyword- based manner (non-structural queries), as the authors of the article "Semantic Search over XML...
10
by: Juha Nieminen | last post by:
Victor Bazarov wrote: If N is very large, std::set can be a real memory hog. If a more memory-efficient algorithm is needed, then a heap can be used. (The exact same type of heap as used in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.