我有这个代码在$ _SESSION中存储“学生”对象:
if(isset($_POST["name"]) && isset($_POST["note"]) && isset($_POST["year"]))
{
$nom = $_POST["name"];
$note = $_POST["note"];
$session = $_POST["year"];
$vec = array("name" => $name, "note" => $note, "year" => $year);
$_SESSION["students"][] = $vec;
echo "The student has been added.<br><br>";
}
然后我将此代码放在另一页中:
function calculateAverage()
{
$av = 0;
$count = 0;
foreach($_SESSION['students'] as $student)
{
$av = $av + $student["note"];
$count = $count + 1;
}
return $av / $count;
}
function bestNote()
{
//$best = array_search(max())
return $best;
}
function worstNote()
{
$worst = min(array_search(["note"], $_SESSION['students']));
return $worst;
}
if(isset($_SESSION['students']))
{
echo "The average note of the group is = " . calculateAverage() . "\n";
echo "The one with the best note is " . bestNote()["name"] . " is " . hauteNote()["note"] . " points.\n";
echo "The one with the worst note is " . worstNote()["name"] . " with " . basseNote()["note"] . " points.\n";
}
如您所见,它还没有完成。我想要做的是能够获得存储在$ _SESSION [“学生”]中的学生的笔记。我怎么能这样做?
感谢您的回答。
答案 0 :(得分:0)
you can access the stored values within a nested array like so:
n
However, you are currently not adding but overwriting data. Use array_push() to add data to an array (your students).
And when adding a student to the array, make sure to give it a name to make it associative so you can simply "call a student":
$studentNote = $_SESSION["students"][YourActiveStudent]["note"];
this way, if the $nom was "Max", you could say
$_SESSION["students"][$nom] = $vec;
to get Max's note (BTW, I assume you are talking about grades or marks, rather than notes?)