Advertisement
MorganF

PHP Scoring

Mar 24th, 2015
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. <?php
  2.  
  3.     $score = array(
  4.         array(
  5.             "uid" => 122,   //uid
  6.             100, //a
  7.             50   //b
  8.         ),
  9.        
  10.         array(
  11.             "uid" => 230,   //uid
  12.             150,    //a
  13.             20      //b
  14.         ),
  15.     );
  16.    
  17.     $a = array(); // new array for type a scores
  18.     $b = array(); // new array for type b scores
  19.     $ab = array();// new array for type ab scores
  20.    
  21.     //We want to add all user scores to the appropriate arrays
  22.     foreach($score as $k => $v) {
  23.         $a[$score[$k]["uid"]] = $score[$k][0]; //Place all A scores into array with users UID as key
  24.         $b[$score[$k]["uid"]] = $score[$k][1];  //Place all A scores into array with users UID as key
  25.         $ab[$score[$k]["uid"]] = $score[$k][0] + $score[$k][1]; //Place A+B scores into array with users UID as key
  26.     }
  27.    
  28.     arsort($a); //Sort a
  29.     arsort($b); //Sort B
  30.     arsort($ab);//Sort A+B
  31.    
  32.     print("User search: 230 \n A:");
  33.     echo array_search("230",array_keys($a)) + 1; //Find user position in array and add 1
  34.     print("\n B:");
  35.     echo array_search("230",array_keys($b)) + 1; //Find user position in array and add 1
  36.     print("\n A+B:");
  37.     echo array_search("230",array_keys($ab)) + 1; //Find user position in array and add 1
  38.    
  39.    
  40.     ####################Expected Output#####################
  41.    ### User search: 230                                   #
  42.    ### A:1                                                #
  43.    ### B:2                                                #
  44.    ### A+B:1                                              #
  45.    ########################################################
  46.  
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement