title: Find the top 10 records from an array on the basis of highest score
body: <p>I can't figure it out on how to fetch top 10 records from an array on the basis of the highest score calculation using php. I fetch the records out from the table, perform the calculation and store the result in an array name score and associating with rest of the query string(Array) so I can carry that calculation into JSON. My issue is I only need to carried top 10 records which has the highest score.</p>
  
  <pre><code>    $gold=$_GET['gold_input'];
      $silver=$_GET['silver_input'];
      $bronze=$_GET['bronze_input'];
      $gdp_value=$_GET['gdp_checked'];
  
  
      $link = new mysqli('localhost', 'root', '','coa123cdb');
      $myArray = array();
  
      $query = "SELECT  * FROM  coa123cdb.Country";
  
      $result = mysqli_query($link, $query)
      or die("Error: ".mysqli_error($link));
  
      $row_cnt = $result-&gt;num_rows;
      if ($result = $link-&gt;query($query)) {
      $tempArray = array();
      $score=array();
  
      $counter=0  ;
      while($row=mysqli_fetch_assoc($result)){
  
        $tempArray['country_name'] = $row['country_name'];
        $tempArray['gdp']=$row['gdp'];
        $tempArray['population']=$row['population'];
        $tempArray['gold']=$row['gold'];
        $tempArray['silver']=$row['silver'];
        $tempArray['bronze']=$row['bronze'];
  
                  if($gdp_value==0)
              {
                          $score=($bronze*$tempArray['bronze'])+($silver*$tempArray['silver'])+($silver*$tempArray['gold']);
  
              }
              else
              {
              $score=($bronze*$tempArray[6]+$silver*$tempArray[5]+$silver*$tempArray[4])*$tempArray[1]/$tempArray[2]/10000;
              }
              $tempArray['score']=$score;
              $data[]=$tempArray;
              $counter++;
          }
  
  
         echo json_encode($data);
  }
  
  $result-&gt;close();
  $link-&gt;close();
  </code></pre>
  
  <p>Please advise what should I need to do in order to pass only top 10 records from an array to JSON object.</p>