How to sort a foreach by an array value?

0

Good afternoon guys, I'm doing a ranking with 10 people, it's showing the 10, but I wanted to display in the descending order through the Value Score

<article class="rank">
<h2>Ranking de Jogadores Online</h2>
<?php
    if(!is_array($aTotalPlayers) || count($aTotalPlayers) == 0){
    echo '<br /><i>No players online!</i>';
  } else {
  ?>
  <table class="table table-striped table-bordered" style="width:600px;" align="center">
    <tr>
        <td><b>#</b></td>
        <td><b>Nickname</b></td>
        <td><b>Score</b></td>
    </tr>
  <?php
    $i = 0;
    foreach($aTotalPlayers as $id => $value){
        if($i >= 10){
            break;
        }
        $i++;
  ?>
    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo htmlentities($value['Nickname']); ?></td>
        <td><?php echo $value['Score']; ?></td>
    </tr>
  <?php
    }
        echo '</table>';
    }

  ?>  

That is, I would have to display the 10 that have the highest score in the descending order, could you help me?

    
asked by anonymous 14.10.2016 / 19:58

1 answer

1

Two ways to solve the problem:

  • In the SQL query, place at the end of your query order by Score DESC .
  • sort your array by grabbing the collection before loop: asort() is decreasing, sort() is increasing: asort($aTotalPlayers) ;
  • 14.10.2016 / 20:49