Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 10 August 2006 - 05:52 PM

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.

#2 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 06:42 PM

try
arsort($points);   //sorts array by values highest to lowest, preserving key=>value relationship.

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

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

#3 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 07:08 PM

To limit how many items are printed out, add this to the code from bltesar:

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>';
   }
}


#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 10 August 2006 - 07:11 PM

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 07:21 PM

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:
$data=array('John'=>7, 'Peter'=>8, 'Jeff'=>2);
arsort($data);


#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 10 August 2006 - 07:24 PM

doesn't arsort preserve the key=>value relationships, and so the assocaition between names and points should be preserved?


How can it if they are in two separate arrays?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 10 August 2006 - 07:29 PM

bool arsort ( array &array [, int sort_flags] )

From the manual it seems it can take 2 arrays

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 10 August 2006 - 07:32 PM

doesn't arsort preserve the key=>value relationships, and so the assocaition between names and points should be preserved?


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

another approach is to set up your array as follows:

$data=array('John'=>7, 'Peter'=>8, 'Jeff'=>2);
arsort($data);


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 10 August 2006 - 07:38 PM

bool arsort ( array &array [, int sort_flags] )

From the manual it seems it can take 2 arrays


No.

"array &array" means the first argument is the array you want to sort, passed by reference.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 08:28 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users