PHP 多维和关联数组列平均值
PHP Multidimensional and Associative Arrays Column Average
大家好 我正在为 PHP 做数组,列出所有列输出并获得测试分数的平均值 但我似乎无法弄清楚获取每列平均值的逻辑。但是我不太确定我这样做是对还是错,因为我的平均输出都是 0,我不知道如何更改它以读取值并计算它。
如能提供帮助,将不胜感激。谢谢。
基本上,我希望输出是这样的;-
RM = Physics : 35, Maths : 30, Chemistry : 39, English : 80,
RM Average Scores: 46
Justin = Physics : 61, Maths : 10, Chemistry : 45, English : 33,
Justin Average Scores: 37.25
Miley = Physics : 25, Maths : 100, Chemistry : 88, English : 60,
Miley Average Scores: 68.25
这是我的数组:-
<?php
$students = array(
"RM" => array("test1" => 35, "test2" => 30,"test3" => 39, "test4" => 80),
"Justin" => array("test1" => 61, "test2" => 10,"test3" => 45, "test4" => 33),
"Miley" => array("test1" => 25, "test2" => 100,"test3" => 88, "test4" => 60),
);
?>
这是我的代码:-
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
if (array_key_exists($i, $the_students)) {
echo average_scores($students, $the_students);
}
echo "</td>";
echo "</tr>";
}
?>
</table>
我使用函数并将其放在代码的末尾:-
<?php
function average_scores($students, $i) {
$total = 0;
$students = array();
foreach ($students as $student => $data) {
$total += $data[$i];
}
return $total / 4;
}
?>
既然你已经在循环了,试试这个
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
$total = 0;
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
$total += $score;
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
echo $total/ count($students[$the_students[$i]]);
echo "</td>";
echo "</tr>";
}
?>
</table>
最佳做法是将 HTML 和 PHP 代码分开。在需要的时候使用 PHP 标记,它会提高代码的可读性 EG:
echo "<b>". $students[$i]. " Average Scores</b>: ";
// convert to
<b><?php $students[$i]; ?> Average Scores</b>:
尝试 foreach
遍历学生得到 name 这是 key 并且还得到 student是关联数组,只获取student的值grades并将它们解构为变量,printf
以更易读的格式打印出来。
echo '<table style="border: 1px solid black">';
echo "<tr><th>Listing All Student Tests and Scores:-</th></tr>";
foreach ($students as $name => $student) {
$grades = array_values($student);
[$Physics, $Maths, $Chemistry, $English] = $grades;
echo "<tr><td>";
printf("%s = Physics : %d, Maths : %d, Chemistry : %d, English : %d<br>
%s Average Scores: %.2f"
,$name, $Physics, $Maths, $Chemistry, $English
,$name, average_scores($grades));
echo "</td></tr>";
}
echo "</table>";
对于平均分,您可以使用 array_sum
对所有成绩求和,然后除以分数。
function average_scores($grades) {
return array_sum($grades)/count($grades);
}
大家好 我正在为 PHP 做数组,列出所有列输出并获得测试分数的平均值 但我似乎无法弄清楚获取每列平均值的逻辑。但是我不太确定我这样做是对还是错,因为我的平均输出都是 0,我不知道如何更改它以读取值并计算它。
如能提供帮助,将不胜感激。谢谢。
基本上,我希望输出是这样的;-
RM = Physics : 35, Maths : 30, Chemistry : 39, English : 80,
RM Average Scores: 46
Justin = Physics : 61, Maths : 10, Chemistry : 45, English : 33,
Justin Average Scores: 37.25
Miley = Physics : 25, Maths : 100, Chemistry : 88, English : 60,
Miley Average Scores: 68.25
这是我的数组:-
<?php
$students = array(
"RM" => array("test1" => 35, "test2" => 30,"test3" => 39, "test4" => 80),
"Justin" => array("test1" => 61, "test2" => 10,"test3" => 45, "test4" => 33),
"Miley" => array("test1" => 25, "test2" => 100,"test3" => 88, "test4" => 60),
);
?>
这是我的代码:-
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
if (array_key_exists($i, $the_students)) {
echo average_scores($students, $the_students);
}
echo "</td>";
echo "</tr>";
}
?>
</table>
我使用函数并将其放在代码的末尾:-
<?php
function average_scores($students, $i) {
$total = 0;
$students = array();
foreach ($students as $student => $data) {
$total += $data[$i];
}
return $total / 4;
}
?>
既然你已经在循环了,试试这个
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
$total = 0;
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
$total += $score;
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
echo $total/ count($students[$the_students[$i]]);
echo "</td>";
echo "</tr>";
}
?>
</table>
最佳做法是将 HTML 和 PHP 代码分开。在需要的时候使用 PHP 标记,它会提高代码的可读性 EG:
echo "<b>". $students[$i]. " Average Scores</b>: ";
// convert to
<b><?php $students[$i]; ?> Average Scores</b>:
尝试 foreach
遍历学生得到 name 这是 key 并且还得到 student是关联数组,只获取student的值grades并将它们解构为变量,printf
以更易读的格式打印出来。
echo '<table style="border: 1px solid black">';
echo "<tr><th>Listing All Student Tests and Scores:-</th></tr>";
foreach ($students as $name => $student) {
$grades = array_values($student);
[$Physics, $Maths, $Chemistry, $English] = $grades;
echo "<tr><td>";
printf("%s = Physics : %d, Maths : %d, Chemistry : %d, English : %d<br>
%s Average Scores: %.2f"
,$name, $Physics, $Maths, $Chemistry, $English
,$name, average_scores($grades));
echo "</td></tr>";
}
echo "</table>";
对于平均分,您可以使用 array_sum
对所有成绩求和,然后除以分数。
function average_scores($grades) {
return array_sum($grades)/count($grades);
}