简体   繁体   English

如何使用 PHP 读取/写入 json 中的单个数组

[英]How can I read/ write to a single array in json with PHP

I have a simple script that can store data in a json file.我有一个简单的脚本,可以将数据存储在 json 文件中。 Every time you save the form it creates a new array.每次保存表单时,它都会创建一个新数组。 I just want to update goal and raised in the same array.我只想更新目标并在同一个数组中提出。

form:形式:

<form action="process.php" method="POST">
Goal:<br>
<input type="text" name="goal">
<br><br/>
Raised:<br>
<input type="text" name="raised">
<br><br>
<input type="submit" value="Submit">
</form>

process.php过程.php

<?php

$myFile = "data.json";
$arr_data = array();

try
{      
    $formdata = array(
        'goal'=> $_POST['goal'],
        'raised'=> $_POST['raised']
    );

    $jsondata = file_get_contents($myFile);

    $arr_data = json_decode($jsondata, true);

    array_push($arr_data,$formdata);

    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

    if(file_put_contents($myFile, $jsondata)) {
            echo 'Data successfully saved';
        }
    else 
            echo "error";

}
catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
}

?>
<?php

$formdata = array(
    'goal'=> $_POST['goal'],
    'raised'=> $_POST['raised']
);


try {

    $fp = fopen('data.json', 'w');
    fwrite($fp, json_encode($formdata));
    fclose($fp);

}
catch(\Exception $e) {
    // Caught Some Exception
}

You can simply use fopen() and fwrite() to open and write to a file.您可以简单地使用fopen()fwrite()打开和写入文件。 The data will be replaced with new data.数据将被新数据替换。

array_push appends. array_push附加。 You want to replace existing, not add, right?您想替换现有的,而不是添加,对吗? So:所以:

<?php

$myFile = "data.json";

try {      
    $arr_data = json_decode(file_get_contents($myFile), true);

    // don't forget to validate first of course:
    $arr_data['goal'] = $_POST['goal'];
    $arr_data['raised'] = $_POST['raised'];

    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

...

To Archive, this file_put_contents won't help much.对于存档,这个 file_put_contents 不会有太大帮助。

Whenever someone posts data from a form, you have to open your file(data.json) in append mode(a).每当有人从表单中发布数据时,您必须以 append 模式(a)打开您的文件(data.json)。 then write your content using fwrite or fopen .然后使用fwritefopen编写您的内容。

In this case, you will get all single data set as a separate array.在这种情况下,您将获得所有单个数据集作为一个单独的数组。 Not a single array不是一个数组

When you do $arr_data = json_decode($jsondata, true);当你做$arr_data = json_decode($jsondata, true); , $arr_data becomes an associative array, this means you can simply do $arr_data['goal'] = $_POST['goal'] or $arr_data['goal'] = $formdata['goal'] , after which you can re-encode the array and save it to your file. , $arr_data成为一个关联数组,这意味着你可以简单地做$arr_data['goal'] = $_POST['goal']$arr_data['goal'] = $formdata['goal'] ,之后你可以重新编码数组并将其保存到您的文件中。 So you only need to switch the line with the array_push() call, to the code I've just explained.因此,您只需将带有array_push()调用的行切换到我刚刚解释的代码。 This works for both 'goal' and 'raised' keys这适用于'goal''raised'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM