Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. $max = array_keys($array,max($array));
  2.  
  3. Array
  4. (
  5. [18] => Array
  6. (
  7. [0] => 77
  8. [1] => 79
  9. [2] => 76
  10. [3] => 78
  11. )
  12. )
  13.  
  14. Array
  15. (
  16. [18] => Array
  17. (
  18. [77] => Array
  19. (
  20. [9] => 18
  21. [8] => 16
  22. [7] => 14
  23. [6] => 12
  24. [5] => 10
  25. [4] => 8
  26. [3] => 6
  27. [2] => 4
  28. [1] => 2
  29. )
  30. [79] => Array
  31. (
  32. [9] => 18
  33. [8] => 17
  34. [7] => 15
  35. [6] => 14
  36. [5] => 11
  37. [4] => 9
  38. [3] => 7
  39. [2] => 5
  40. [1] => 3
  41. )
  42. [76] => Array
  43. (
  44. [9] => 18
  45. [8] => 16
  46. [7] => 14
  47. [6] => 12
  48. [5] => 10
  49. [4] => 8
  50. [3] => 6
  51. [2] => 4
  52. [1] => 2
  53. )
  54. [78] => Array
  55. (
  56. [9] => 18
  57. [8] => 17
  58. [7] => 15
  59. [6] => 13
  60. [5] => 11
  61. [4] => 9
  62. [3] => 7
  63. [2] => 5
  64. [1] => 3
  65. )
  66. )
  67. )
  68.  
  69. foreach ($ties as $comparekey => &$compareval) {
  70. $tie_loop = 0;
  71. for ($m = 9; $m >= 1; $m--) {
  72. $compare = array();
  73. foreach ($compareval as $tie) {
  74. $compare[$tie] = $tie_perhole[$comparekey][$tie][$m];
  75. }
  76. $row = array_keys($compare,max($compare));
  77.  
  78. if (count($row) == 1) {
  79. $indexties = array_search($row[0], $ties[$comparekey]);
  80. unset($ties[$comparekey][$indexties]);
  81. // Now update this "winners" finishing position in a sorted array
  82. // This is a multidimensional array too, with custom function...
  83. $indexresults = searchForId($row[0], $comp_results_arr);
  84. $comp_results_arr[$indexresults][position] = $tie_loop;
  85. $tie_loop++;
  86. }
  87. // I think I need conditions here to filter if a subset of players tie
  88. // Other than count($row) == 1
  89. // And possibly splitting out into multiple $ties arrays for each thread...
  90.  
  91. if (empty($ties[$comparekey])) {
  92. break;
  93. }
  94. }
  95. }
  96. usort($comp_results_arr, 'compare_posn_asc');
  97. foreach($comp_results_arr as $row) {
  98. //echo an HTML table...
  99. }
  100.  
  101. <?php
  102. /**
  103. * Author : Carlos Alaniz
  104. * Email : Carlos.glvn1993@gmail.com
  105. * Porpuse : Stackoverflow example
  106. * Date : Aug/04/2015
  107. **/
  108.  
  109.  
  110. $golfers = [
  111. "A" => [1,5,9,1,1,2,3,4,9],
  112. "B" => [2,6,4,2,4,4,1,9,3],
  113. "C" => [3,4,9,8,1,1,5,1,3],
  114. "D" => [1,5,1,1,1,5,4,5,8]
  115. ];
  116.  
  117. //Iterate over scores.
  118. function get_winners(&$golfers, $hole = 9){
  119. $positions = array(); // The score numer is the key!
  120. foreach ($golfers as $golfer=>$score ) { // Get key and value
  121. $score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
  122. $total_score = (string)array_sum($score_sub); // Get the key
  123. if(!isset($positions[$total_score])){
  124. $positions[$total_score] = array(); // Make array
  125. }
  126. $positions[$total_score][] = $golfer; // Add Golpher to score.
  127. }
  128. ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
  129. return array(end($positions), key($positions)); // The last shall be first
  130. }
  131.  
  132. //Recursion is Awsome
  133. function getWinner(&$golfers, $hole = 9){
  134. if ($hole == 0) return;
  135. $winner = get_winners($golfers,$hole); // Get all ties, if any.
  136. if(count($winner[0]) > 1){ // If theirs ties, filter again!
  137. $sub_golfers =
  138. array_intersect_key($golfers,
  139. array_flip($winner[0])); // Only the Worthy Shall Pass.
  140. $winner = getWinner($sub_golfers,$hole - 1); // And again...
  141. }
  142. return $winner; // We got a winner, unless they really tie...
  143. }
  144.  
  145. echo "<pre>";
  146. print_R(getWinner($golfers));
  147. echo "</pre>";
  148.  
  149. Array
  150. (
  151. [0] => Array
  152. (
  153. [0] => A
  154. [1] => B
  155. [2] => C
  156. )
  157.  
  158. [1] => 35
  159. )
  160.  
  161. Array
  162. (
  163. [0] => Array
  164. (
  165. [0] => B
  166. [1] => C
  167. )
  168.  
  169. [1] => 32
  170. )
  171.  
  172. Array
  173. (
  174. [0] => Array
  175. (
  176. [0] => C
  177. )
  178.  
  179. [1] => 31
  180. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement