Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $max = array_keys($array,max($array));
- Array
- (
- [18] => Array
- (
- [0] => 77
- [1] => 79
- [2] => 76
- [3] => 78
- )
- )
- Array
- (
- [18] => Array
- (
- [77] => Array
- (
- [9] => 18
- [8] => 16
- [7] => 14
- [6] => 12
- [5] => 10
- [4] => 8
- [3] => 6
- [2] => 4
- [1] => 2
- )
- [79] => Array
- (
- [9] => 18
- [8] => 17
- [7] => 15
- [6] => 14
- [5] => 11
- [4] => 9
- [3] => 7
- [2] => 5
- [1] => 3
- )
- [76] => Array
- (
- [9] => 18
- [8] => 16
- [7] => 14
- [6] => 12
- [5] => 10
- [4] => 8
- [3] => 6
- [2] => 4
- [1] => 2
- )
- [78] => Array
- (
- [9] => 18
- [8] => 17
- [7] => 15
- [6] => 13
- [5] => 11
- [4] => 9
- [3] => 7
- [2] => 5
- [1] => 3
- )
- )
- )
- foreach ($ties as $comparekey => &$compareval) {
- $tie_loop = 0;
- for ($m = 9; $m >= 1; $m--) {
- $compare = array();
- foreach ($compareval as $tie) {
- $compare[$tie] = $tie_perhole[$comparekey][$tie][$m];
- }
- $row = array_keys($compare,max($compare));
- if (count($row) == 1) {
- $indexties = array_search($row[0], $ties[$comparekey]);
- unset($ties[$comparekey][$indexties]);
- // Now update this "winners" finishing position in a sorted array
- // This is a multidimensional array too, with custom function...
- $indexresults = searchForId($row[0], $comp_results_arr);
- $comp_results_arr[$indexresults][position] = $tie_loop;
- $tie_loop++;
- }
- // I think I need conditions here to filter if a subset of players tie
- // Other than count($row) == 1
- // And possibly splitting out into multiple $ties arrays for each thread...
- if (empty($ties[$comparekey])) {
- break;
- }
- }
- }
- usort($comp_results_arr, 'compare_posn_asc');
- foreach($comp_results_arr as $row) {
- //echo an HTML table...
- }
- <?php
- /**
- * Author : Carlos Alaniz
- * Email : Carlos.glvn1993@gmail.com
- * Porpuse : Stackoverflow example
- * Date : Aug/04/2015
- **/
- $golfers = [
- "A" => [1,5,9,1,1,2,3,4,9],
- "B" => [2,6,4,2,4,4,1,9,3],
- "C" => [3,4,9,8,1,1,5,1,3],
- "D" => [1,5,1,1,1,5,4,5,8]
- ];
- //Iterate over scores.
- function get_winners(&$golfers, $hole = 9){
- $positions = array(); // The score numer is the key!
- foreach ($golfers as $golfer=>$score ) { // Get key and value
- $score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
- $total_score = (string)array_sum($score_sub); // Get the key
- if(!isset($positions[$total_score])){
- $positions[$total_score] = array(); // Make array
- }
- $positions[$total_score][] = $golfer; // Add Golpher to score.
- }
- ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
- return array(end($positions), key($positions)); // The last shall be first
- }
- //Recursion is Awsome
- function getWinner(&$golfers, $hole = 9){
- if ($hole == 0) return;
- $winner = get_winners($golfers,$hole); // Get all ties, if any.
- if(count($winner[0]) > 1){ // If theirs ties, filter again!
- $sub_golfers =
- array_intersect_key($golfers,
- array_flip($winner[0])); // Only the Worthy Shall Pass.
- $winner = getWinner($sub_golfers,$hole - 1); // And again...
- }
- return $winner; // We got a winner, unless they really tie...
- }
- echo "<pre>";
- print_R(getWinner($golfers));
- echo "</pre>";
- Array
- (
- [0] => Array
- (
- [0] => A
- [1] => B
- [2] => C
- )
- [1] => 35
- )
- Array
- (
- [0] => Array
- (
- [0] => B
- [1] => C
- )
- [1] => 32
- )
- Array
- (
- [0] => Array
- (
- [0] => C
- )
- [1] => 31
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement