mitwess Posted December 21, 2009 Share Posted December 21, 2009 so i have an array of 20 numerical values (0-100) that i need to order from highest to lowest and then i need to retrieve the keys of the top ten values. $array1 = array($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10,$a11,$a12,$a13,$a14,$a15,$a16,$a17,$a18,$a19,$a20); arsort($array1); <? print_r($array1);?> which yields Array ( [4] => 74 [12] => 70 [19] => 70 [0] => 66 [3] => 66 [10] => 63 [2] => 62 [8] => 57 [11] => 57 [15] => 50 [14] => 50 [9] => 50 [1] => 50 [5] => 50 [17] => 30 [7] => 30 [18] => 30 [13] => 23 [16] => 23 [6] => 23 ) i figured how to return the key of the highest value function max_extract($array1) { return array_keys($array1, max($array1)); } $max_key= max($array1); but i can't figure out how to get the remainder of the top ten keys (i.e. 19,0,3,10,2,8,11,15,14). any ideas? Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/ Share on other sites More sharing options...
BahBah Posted December 21, 2009 Share Posted December 21, 2009 What are you using the top 10 keys for ? You could just loop through the first 10 elements and do what you like with them. for ($i=0;$i<10;$i++) { $dosomething = array1[$i]; } Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981902 Share on other sites More sharing options...
mitwess Posted December 21, 2009 Author Share Posted December 21, 2009 i have two arrays with twenty scores (0-100) each that i need to compare (and @3000 more of these comparisons i need to make from a database). i want to take the top ten numerical scores from each array, and see how many of the same keys show up in the top 10 from each array. Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981930 Share on other sites More sharing options...
BahBah Posted December 21, 2009 Share Posted December 21, 2009 I'm sorry I'm unclear what you mean. Do you mean you have 3000 sets of arrays containing 20 scores ? and you want to see how many of the same scores show up ? Are each set of 20 scores a row in the database ? Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981933 Share on other sites More sharing options...
mitwess Posted December 22, 2009 Author Share Posted December 22, 2009 yes i have two sets of 1500 database records/rows. each record has 20 scores/fields. each record has a related record that i want to compare it with. record 1a (20 mysql fields of scores) 82 12 56 86.... record 1b (20 mysql fields of scores) 93 38 20 48... so i want to make 1500 comparisons, one for each set of related records. i want to take the top 10 scores of each related record, and see how many of the same key scores (fields) show up on each top 10 list. currently i'm fetching each record from the database and turning it into an array. but then i'm stuck on figuring out how i can make a new array of just the top ten numerical value keys (which i will then compare to see how many matching top 10 keys/fields exist for each pair of related records). Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981949 Share on other sites More sharing options...
BahBah Posted December 22, 2009 Share Posted December 22, 2009 // while loop your recordset of all 3000 rows here $tempRow = arsort($currentDBrow); for ($i=0;$i<10;$i++) { $top10array[] = $tempRow[$i]; } // end while loop You should now have the top 10 scores over all 3000 records in a single array. You won't know who scored what, but you will know all the scores and will be able to say how many people got the same score. Hrm just tested it, and array_unique won't do it. I'll have a think. Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981954 Share on other sites More sharing options...
BahBah Posted December 22, 2009 Share Posted December 22, 2009 array_count_values should do it. print_r( count_array_values($top10array) ); It should return as: [score] => [numOccurences] Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-981961 Share on other sites More sharing options...
BahBah Posted December 26, 2009 Share Posted December 26, 2009 Did this work out for you ? I appreciate that it wasn't the most elegant solution, but should have achieved what you wanted. You didn't state if this was a regularly used function or if it was a one off ... Link to comment https://forums.phpfreaks.com/topic/185947-retrieving-more-than-one-max-key-from-an-array/#findComment-984106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.