Jump to content

Archived

This topic is now archived and is closed to further replies.

Mateobus

Sorting Arrays and data that isn't in a database.

Recommended Posts

Ok My Setup is as follows: there are two arrays, one with a name value, the other one with a number of points.  I want to make a top ten list of the points.
so it looks like this.

$name[1] = "John";
$name[2] = "Peter";
$name[3]="Jeff";

$points[1] = 7;
$points[2]= 8;
$points[3]= 2;

I want to print the following list

1.Peter -8
2. John -7
3. Jeff -2

I can change the arrays to something more logical if necesary.

Share this post


Link to post
Share on other sites
try
[code]
arsort($points);  //sorts array by values highest to lowest, preserving key=>value relationship.

foreach($points as $key=>$value)
{
    echo $name[$key].'-'.$value.'<br>';
}
[/code]

of course, you could achieve your objective in many differnet ways.

Share this post


Link to post
Share on other sites
To limit how many items are printed out, add this to the code from [b]bltesar[/b]:

[code=php:0]
arsort($points);  //sorts array by values highest to lowest, preserving key=>value relationship.

// How many results to show
$limit = 10;

for($i = 1; $i <= $limit, $i++){
  foreach($points as $key=>$value)
  {
    echo $i.". ".$name[$key].'-'.$value.'<br>';
  }
}
[/code]

Share this post


Link to post
Share on other sites
You need to put this data into the same array, otherwise when you sort the array with the points in it you will lose the association with the names.

E.g. $name_and_points = array(array("name"=>"John", "points"=>7),
                                         array("name"=>"Peter", "points"=>8 ),
                                         array("name"=>"Jeff", "points"=>2));

Then do a usort on the points column.

Share this post


Link to post
Share on other sites
doesn't arsort preserve the key=>value relationships, and so the assocaition between names and points should be preserved?

another approach is to set up your array as follows:
[code]
$data=array('John'=>7, 'Peter'=>8, 'Jeff'=>2);
arsort($data);
[/code]

Share this post


Link to post
Share on other sites
[quote author=bltesar link=topic=103746.msg413418#msg413418 date=1155237717]
doesn't arsort preserve the key=>value relationships, and so the assocaition between names and points should be preserved?
[/quote]

How can it if they are in two separate arrays?

Share this post


Link to post
Share on other sites
[quote]bool arsort ( array &array [, int sort_flags] )[/quote]
From the manual it seems it can take 2 arrays

Share this post


Link to post
Share on other sites
[quote author=bltesar link=topic=103746.msg413418#msg413418 date=1155237717]
doesn't arsort preserve the key=>value relationships, and so the assocaition between names and points should be preserved?[/quote]

Yes, it works fine. But I would go with your alternative offering below which keeps the names and values together in an easy-to-sort 1 dimensional array

[quote]
another approach is to set up your array as follows:
[code]
$data=array('John'=>7, 'Peter'=>8, 'Jeff'=>2);
arsort($data);
[/code]

[/quote]

Share this post


Link to post
Share on other sites
[quote author=businessman332211 link=topic=103746.msg413429#msg413429 date=1155238167]
[quote]bool arsort ( array &array [, int sort_flags] )[/quote]
From the manual it seems it can take 2 arrays
[/quote]

No.

"array &array" means the first argument is the array you want to sort, passed by reference.

Share this post


Link to post
Share on other sites
yes, it will still work with two arrays, but you only need to pass the $points array to the arsort function. 

both arrays share the same key, so you sort $points by its values(not keys) and then go through $points and use the keys to reference back to the $names array.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.