Jump to content

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.
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.
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]
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.
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]
[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?
[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]
[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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.