Mateobus Posted August 10, 2006 Share Posted August 10, 2006 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 list1.Peter -82. John -73. Jeff -2I can change the arrays to something more logical if necesary. Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/ Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72651 Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72665 Share on other sites More sharing options...
king arthur Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72666 Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 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 Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72672 Share on other sites More sharing options...
king arthur Posted August 10, 2006 Share Posted August 10, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72678 Share on other sites More sharing options...
Ninjakreborn Posted August 10, 2006 Share Posted August 10, 2006 [quote]bool arsort ( array &array [, int sort_flags] )[/quote]From the manual it seems it can take 2 arrays Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72682 Share on other sites More sharing options...
Barand Posted August 10, 2006 Share Posted August 10, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72683 Share on other sites More sharing options...
Barand Posted August 10, 2006 Share Posted August 10, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72687 Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17156-sorting-arrays-and-data-that-isnt-in-a-database/#findComment-72705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.