我有一个如下所示的数组:
阵列
Array
(
[Team 1] => Array
(
[wins] => 60
)
[Team 2] => Array
(
[wins] => 12
)
[Team 3] => Array
(
[wins] => 79
)
[Team 4] => Array
(
[wins] => 36
)
[Team 5] => Array
(
[wins] => 29
)
)
然后我跨越了使用php foreach
HTML
<span><?php echo ['Team 1']['wins'] ?></span>
<span><?php echo ['Team 2']['wins'] ?></span>
<span><?php echo ['Team 3']['wins'] ?></span>
<span><?php echo ['Team 4']['wins'] ?></span>
<span><?php echo ['Team 5']['wins'] ?></span>
我希望在每个团队旁边显示一些文字,给他们排名1-5,基于他们的&#34;胜利&#34;值。我希望输出看起来像这样。有没有办法计算&#34;胜利&#34;并使用wins值给出1到5的排名?
预期结果
<span><?php echo "#2" ['Team 1']['wins'] ?></span>
<span><?php echo "#5" ['Team 2']['wins'] ?></span>
<span><?php echo "#1" ['Team 3']['wins'] ?></span>
<span><?php echo "#3" ['Team 4']['wins'] ?></span>
<span><?php echo "#4" ['Team 5']['wins'] ?></span>
答案 0 :(得分:0)
您可以使用array multi_sort
<强>阵列强>
<?php
$inventory =
Array
(
'Team 1'=> Array
(
'wins' => 60
),
'Team 2' => Array
(
'wins' => 12
),
'Team 3' => Array
(
'wins' => 79
),
'Team 4' => Array
(
'wins' => 36
),
'Team 5' => Array
(
'wins' => 29
),
);
?>
<强> PHP 强>
<?php
$inventory2 = $inventory;
$kol = array();
$a = 1;
array_multisort($inventory, SORT_DESC);
foreach ($inventory as $key=>$value)
{
$kol[$key] = $value;
$kol[$key]['rank'] = $a++;
}
$ar = array_merge($inventory2,$kol);
?>
<强> HTML 强>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php foreach ($ar as $key => $value) { ?>
<span><?php echo $key ?></span><br> WINS : <?php echo $value['wins'] ?></span>
</br>Rank : <?php echo $value['rank'] ?></span>
</br></br>
<?php } ?>
</body>
</html>
试试这段代码,它应该从最大到最小排序。