Please help with the idea of implementing data changes in csv files
There is a form for finding a value in a multidimensional array.
<form method=post name=form4> <table><tbody><tr> <td><input type=text name=fieldid /></td> <td><input type=submit name=form4 /></td> </tr></table></tbody></form> Self data search and return them to a new form for further editing
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form4'])) { $needle = $_POST['fieldid']; $result = array_filter($get_csv, function($innerArray){ global $needle; return ($innerArray[0] == $needle);}); $ get_csv // I have a multidimensional array from a csv file
echo "<form method=post name=form5><table><tbody><tr>"; foreach ($result as $val) { echo "<td><input type=text name=field0 value=\"" . $val[0] . "\"/></td>"; echo "<td><input type=text name=field1 value=\"" . $val[1] . "\"/></td>"; echo "<td><input type=text name=field2 value=\"" . $val[2] . "\"/></td>"; echo "<td><input type=text name=field3 value=\"" . $val[3] . "\"/></td>";} echo " <td><input type=submit name=form5/></td>"; echo "</tr></table></tbody></form>"; if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form5'])) { $field0 = $_POST['field0'];$field1 = $_POST['field1'];$field2 = $_POST['field2'];$field3 = $_POST['field3']; $f=fopen("old.csv","r"); $r=fopen("new.csv","w"); while (!feof($f)) { $s=fgets($f); $a=explode(";",$s); if ($a[0]==$field0) $a[1]=$field1; fputs($r,implode(";",$a); } fclose($f); fclose($r); }); } The replacement itself is carried out by rewriting a file, and not the current one, but another one. Now I am reading from the reference book that with the help of array_replace it is possible to change the value in the array, but how can I replace only the necessary line in the file without overwriting it? As far as I know using php, it is possible to add data only to the end of the file.
PS I wrote the post from the phone, I can not format it. Post was written with mobile, can not style it