I have a problem showing the field of the array 'notes' since it does not print the numbers generated by the 'addNotes' function.
The code loads the preloaded data and assigns random numbers with the 'addNotes' function to the 'notes' fields of the array and finally shows the data of the array in showStudentsList ().
<?php session_start();?>
<html>
<body>
<?php
if(!existDataInSession()){
initializePreloadedData();
}
function existDataInSession(){
return $_SESSION['data'] != NULL;
}
function initializePreloadedData(){
$person1= [
'name' => 'person1',
'notes' => []
];
$person2= [
'name' => 'person2',
'notes' => []
];
$data=[$person1,$person2];
$_SESSION['data'] = $data;
}
function addNotes(){
foreach ($data as $key => $value) {
$data[$key]['notes'] = random_int(0,100);
}
}
addNotes();
showStudentsList();
function showStudentsList(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo $student['name'] . " ";
echo $student['notes'];
echo implode($student['notes']);
echo "<br>";
}
}
//Result
//person1 Array
//person2 Array
?>
</body>
</html>