PHP按键写新数组值或创建新行

I want to read single array values with php and save them acoording to a key. If this key does not exists, a new entry should be created.

Here is my language fileTo read and write:

$LNG['name'] = array(
    1 => 'test',
    2 => 'test1',
    3 => 'test2',
    4 => 'test3',
    5 => 'test4',
);

Here is are Example

<?php
$include("TECH.php");

$name = $LNG['name'][$_GET['id']];
?>
<html>
<head>
    <title>Editpage</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    <table>
<tr>
    <td>Name</td><td><input name="name" type="text" value="{$name}"/></td>
</tr>    
</table>
    <p><input type="submit" name="submit" value="Save"></p>
</form>
</body>
</html>

and by submit change value from the selected key

Example The key is 1 by Submit change value from key 1 to

$LNG['name'] = array(
    1 => 'i am edited',
    2 => 'test1',
    3 => 'test2',
    4 => 'test3',
    5 => 'test4',
);

For save i have tested with this

$file = 'TECH.php';
// The new person to add to the file
$saveedit = $LNG['tech'][$_GET['id']] = $_POST['name_ger'];
// Write the contents to the file, 
file_put_contents($file, $saveedit, FILE_APPEND | LOCK_EX);

and this add the edit name nothing he add a new entry

$LNG['name'] = array(
    1 => 'test',
    2 => 'test1',
    3 => 'test2',
    4 => 'test3',
    5 => 'test4',
);editvalue

So you want to store data in a file, in an array that PHP can “read” by simply including the file?

Then to modify that data, you can simply include the file as well, so that the variable/data structure $LNG['name'] is present in your script that handles the submitted form.

To modify values, you simply overwrite them:

$LNG['name'][$idfromform] = $valuefromform;

Not necessary to check whether the key exists or not, PHP will create it automatically if it doesn’t.

And then, to get your modified data structure written back to the file, you simply use var_export and file_put_contents.

You might want to think about limiting what keys can be created in the array somehow though. And protect that form from being used by just any user of your site, otherwise you risk getting undesired side effects or might even create a security hole. But that largely depends on what you’ll do with the data structure afterwards.