通過$總陣列這將循環和分配的所有值小於10到一個數組然後循環通過該陣列打印出多少出總的爲每個小於10
$total = [
0 => [
'name' => 'Hello',
'email' => '[email protected]',
'score' => 10,
'time' => 15
],
1 => [
'name' => 'Hello',
'email' => '[email protected]',
'score' => 58,
'time' => 10
],
2 => [
'name' => 'Test',
'email' => '[email protected]',
'score' => 10,
'time' => 12
],
3 => [
'name' => 'Stack',
'email' => '[email protected]',
'score' => 90,
'time' => 20
],
4 => [
'name' => 'Test',
'email' => '[email protected]',
'score' => 50,
'time' => 40
],
];
$sizeOfArray = count($total);
$arrayOfScoresLessThanTen = [];
foreach($total as $value) {
if($value['score'] <= 10) {
if(isset($arrayOfScoresLessThanTen[$value['score']])) {
$arrayOfScoresLessThanTen[$value['score']] ++;
}
else {
$arrayOfScoresLessThanTen[$value['score']] = 1;
}
}
}
foreach($arrayOfScoresLessThanTen as $key => $scoresLessThanTen) {
echo 'Score: '. $key . ' number scored out of total: '. $scoresLessThanTen . '/'.$sizeOfArray;
}
這已經過測試,並與我創建的數組$ total一起工作,以匹配您在表中使用的數組。我添加了我的$ total數組,以便您可以自己測試它。
你只想知道有多少得分10?或者你希望它是動態的,並且每個數字都有一個百分比? –
是在行總數中有多少得分爲10,9,8,7,6,5,4,3,2,1 – Roger