I have a form which takes a student name, subject and age. When submitted, it saves the data as an array in txt file. Next time, when I put new data and submit it, it creates a new array in that txt file instead of appending it to the previous array.
<?php
if(!empty($_GET)){
$student = [];
$student['name'] = $_GET['name'];
$student['subject'] = $_GET['subject'];
$student['age'] = $_GET['age'];
$studentArray = [];
array_push($studentArray, $student);
$str = print_r($studentArray, true);
file_put_contents('student.txt', $str, FILE_APPEND);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="GET">
<label for="name">Name:</label>
<input type="text" name="name" id="name">
<label for="name">Subject:</label>
<input type="text" name="subject" id="subject">
<label for="name">Age:</label>
<input type="number" name="age" id="age">
<input type="submit" name="submitButton">
</form>
</body>
</html>
output looks like this:
however I want to save it like below:
How could I do that?


