0
<?php
$ranks = array(
"Alex 1"    =>" $team0",
"Alex 2"    => "$team1",
"Dave 1"    => "$team2",
"Dave 2"    => "$team3",
"Grant 1"   => "$team4",
"Grant 2"   => "$team5",
"Jason 1"   => "$team6",
"Jason 2"   => "$team7",
"Jessica 1" => "$team8",
"Jessica 2" => "$team9",
"Kevin 1"   => "$team10",
"Kevin 2"   => "$team11",
"Kris 1"    => "$team12",
"Kris 2"    => "$team13",
"Mark F 1" => "$team14",
"Mark F 2" => "$team15",
"Mark P 1" => "$team16",
"Mark P 2" => "$team17",
"Tim 1"     => "$team18",
"Tim 2"     => "$team19",
"Travis 1" => "$team20",
"Travis 2" => "$team21",
"Trevor 1" => "$team22",
"Trevor 2" => "$team23");
ksort($ranks);
foreach($ranks as $key => $val) {echo " ( $key ... $val\n pts ) ";}
?>

すべての結果をエコーするのではなく、上位5つの結果をhtmlテーブルの行にエコーし、各結果を独自の結果にします...

4

3 に答える 3

3

どうぞ!

// Sort by value descending, while maintaining key association
uasort($ranks, function($a,$b) {
    return $a < $b ? -1 : ($a == $b ? 0 : 1);
});

// Get top 5 results
$top5 = array_slice($ranks, 0, 5);

// Display results in a table
echo "<table>";
foreach($top5 as $key => $val) {
    echo "<tr><td>$key</td><td>$val pts</td></tr>\n ";
}
echo "</table>";
于 2012-11-22T04:25:18.670 に答える
0

試す

asort($ranks);
$input = array_unique($ranks,SORT_NUMERIC);
array_splice($input , 5); 
echo "<table>";
foreach($input as $k => $v) {
  echo "<tr><td>$k</td>
            <td>$v pts</td>
        </tr>";
}
echo "</table>";

参照:

asort

array_uniuqe

array_splice

于 2012-11-22T04:41:00.400 に答える
0
<?php
    $ranks = array(
    "Alex 1"    =>" $team0",
    "Alex 2"    => "$team1",
    "Dave 1"    => "$team2",
    "Dave 2"    => "$team3",
    "Grant 1"   => "$team4",
    "Grant 2"   => "$team5",
    "Jason 1"   => "$team6",
    "Jason 2"   => "$team7",
    "Jessica 1" => "$team8",
    "Jessica 2" => "$team9",
    "Kevin 1"   => "$team10",
    "Kevin 2"   => "$team11",
    "Kris 1"    => "$team12",
    "Kris 2"    => "$team13",
    "Mark F 1" => "$team14",
    "Mark F 2" => "$team15",
    "Mark P 1" => "$team16",
    "Mark P 2" => "$team17",
    "Tim 1"     => "$team18",
    "Tim 2"     => "$team19",
    "Travis 1" => "$team20",
    "Travis 2" => "$team21",
    "Trevor 1" => "$team22",
    "Trevor 2" => "$team23");

    ksort($ranks);

    $table = "<table>";
    $index = 0;
    foreach($ranks as $key => $val) 
    { 
      if(index < 5)
        $table .= "<tr><td>$key</td><td>$val pts</td></tr>";
      else
       break;

     index++;
    }

    $table .= "</table";

    echo $table;

    ?>
于 2012-11-22T04:27:36.330 に答える