canabatz Posted January 5, 2015 Share Posted January 5, 2015 hi all, im trying to get best results from this list name--------good---------best time carl------------1 ---------14 john------------2 ---------25 benny----------2 ---------21 good represent right answer and bet time represent speed. i need to find who answer most and the least time. so the best from the example is 2-21 i have put the results in array and found the max(good) and the min(best time) how can i found that benny won in table example, he had 2 good and is best time is better then john, carl is out because he have only 1 good. thanks Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted January 5, 2015 Share Posted January 5, 2015 You're in the wrong forum, this is obviously an SQL question. ... ORDER BY good DESC, best_time ASC LIMIT 1 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 5, 2015 Solution Share Posted January 5, 2015 Just in case you aren't using a database, you can sort the array by good(DESC), time(ASC) then take the the first element. $results = [ ['name'=>'carl', 'good'=>1, 'time'=>14], ['name'=>'john', 'good'=>2, 'time'=>25], ['name'=>'benny', 'good'=>2, 'time'=>21] ]; usort($results, 'resultSort'); echo $results[0]['name']; // benny function resultSort($a, $b) { // compare by 'good' DESC $x = $b['good'] - $a['good']; if ($x==0) { // if same, compare by 'time' ASC return $a['time'] - $b['time']; } else return $x; } Quote Link to comment Share on other sites More sharing options...
canabatz Posted January 8, 2015 Author Share Posted January 8, 2015 the sorting is not workig Quote Link to comment Share on other sites More sharing options...
canabatz Posted January 8, 2015 Author Share Posted January 8, 2015 PHP Parse error: syntax error, unexpected '[' in /home5/bingoga1/public_html/shiluvyco/trivika/calct.php on line 5 Quote Link to comment Share on other sites More sharing options...
canabatz Posted January 8, 2015 Author Share Posted January 8, 2015 i changed it to: $results = array( array('name'=>'carl', 'good'=>4, 'time'=>14), array('name'=>'john', 'good'=>4, 'time'=>25), array('name'=>'benny', 'good'=>5, 'time'=>21) ); usort($results, 'resultSort'); echo $results[0]['name']; // benny Quote Link to comment Share on other sites More sharing options...
Barand Posted January 8, 2015 Share Posted January 8, 2015 The [..] syntax instead of array() requires version 5.4+. Quote Link to comment Share on other sites More sharing options...
canabatz Posted January 8, 2015 Author Share Posted January 8, 2015 thanks Barand, works perfect!! Quote Link to comment 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.