将JSON保存到文件,如果存在则更改输出

时间:2016-04-09 11:56:41

标签: php arrays json save

我使用PHP将数组数据保存到.json文件中。然后,某些JS在其他地方调用.json文件并将其放入变量。

这里是PHP的代码:

$fetch = mysqli_query($connection, "SELECT * FROM mrb_prprty WHERE `nationalid` = '". $nationalid ."' AND `author` = '" .$username. "'");
$return_arr = array();

while ($row = mysqli_fetch_array($fetch)) {
    $row_array['firstName'] = $row['fname'];
    $row_array['lastName'] = $row['lname'];
    $row_array['nationality'] = $row['nationality'];
    $row_array['phone'] = $row['mobile'];
    $row_array['email'] = $row['email'];
    $row_array['street'] = $row['street'];
    $row_array['towncity']  = $row['towncity'];
    $row_array['postcode']= $row['postcode'];
    $row_array['county'] = $row['county'];
    $row_array['country'] = $row['country'];
    $row_array['rentpw'] = $row['rentpw'];
    $row_array['nationalid'] = $row['nationalid'];
    $row_array['passport'] = $row['passport'];
    $row_array['isFavorite'] = $row['favourite'];

    array_push($return_arr,$row_array);
}

$json_mrb = json_encode($return_arr);

$myFile = "userdata/". $username .".json";

$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $json_mrb."\n");

当第一次执行此操作时,它将检测文件是否存在&根据结果​​创建新文件或附加到现有文件。

这是' .json'的预览。首次运行上面的PHP代码后的文件:

[{"firstName":"Ryan","lastName":"Butterworth","nationality":"rKqF01JNdAB1\/EjPW1sulcHzA+Lqp3y\/aaNtpixFphA=","phone":"6jAbOoIal2Eh73neklZgXcA2jJ3831uRiJWE74fADD8=","email":"gpLkiFg4ju4jv4QK6nftnPCqmzdENbsDt4ayI4L1rrkXvg0VJkxmP9Ac57fs4Ar\/","street":"22 Newton Drive","towncity":"Blackpool","postcode":"FY38BS","county":"England","country":"England","rentpw":"VCXaMj\/V6YNExbREuI08eOuINutZRxcx4vv5s2C2FR4=","nationalid":"dNDgHnJJg4rFgbb6qOEC\/dVlnJUNWLCZASo9X8GP5EE=","passport":"123456","isFavorite":""}]

我遇到的问题是,当您第二次运行PHP代码时,.json文件将如下所示:

[{"firstName":"Ryan","lastName":"Butterworth","nationality":"rKqF01JNdAB1\/EjPW1sulcHzA+Lqp3y\/aaNtpixFphA=","phone":"6jAbOoIal2Eh73neklZgXcA2jJ3831uRiJWE74fADD8=","email":"gpLkiFg4ju4jv4QK6nftnPCqmzdENbsDt4ayI4L1rrkXvg0VJkxmP9Ac57fs4Ar\/","street":"22 Newton Drive","towncity":"Blackpool","postcode":"FY38BS","county":"England","country":"England","rentpw":"VCXaMj\/V6YNExbREuI08eOuINutZRxcx4vv5s2C2FR4=","nationalid":"dNDgHnJJg4rFgbb6qOEC\/dVlnJUNWLCZASo9X8GP5EE=","passport":"123456","isFavorite":""}]
[{"firstName":"Ryan","lastName":"Butterworth","nationality":"rKqF01JNdAB1\/EjPW1sulcHzA+Lqp3y\/aaNtpixFphA=","phone":"6jAbOoIal2Eh73neklZgXcA2jJ3831uRiJWE74fADD8=","email":"gpLkiFg4ju4jv4QK6nftnPCqmzdENbsDt4ayI4L1rrkXvg0VJkxmP9Ac57fs4Ar\/","street":"22 Newton Drive","towncity":"Blackpool","postcode":"FY38BS","county":"England","country":"England","rentpw":"VCXaMj\/V6YNExbREuI08eOuINutZRxcx4vv5s2C2FR4=","nationalid":"dNDgHnJJg4rFgbb6qOEC\/dVlnJUNWLCZASo9X8GP5EE=","passport":"123456","isFavorite":""}]

Javascript加载' .json' file作为变量,如:var contacts = //fileoutputhere,但正如您所见,.json文件现在有两个数组列表,因此会遇到错误。

而不是像这样保存到文件: [{ ...info... }] [{ ...info... }]

我希望它像这样保存: [{ ...info... },{ ...info... }]

我知道我必须以某种方式调整PHP,但不知道如何才能做到这一点。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用您的方法,目标文件中有多个JSON字符串,而不是具有唯一的JSON。

以这种方式启动代码:

$myFile = "userdata/". $username .".json";

if( file_exists( $myFile ) )
{
    $data = file_get_contents( $myFile );
    $return_arr = json_decode( $data, TRUE );
}
else
{
    $return_arr = array();
}

此时,如果文件存在,则在$return_arr中您之前保存了值。我使用json_encode的第二个参数返回一个关联数组,以便与新添加的值保持一致,但在您的情况下,它是完全可选的。如果该文件不存在,$return_arr是一个空数组。

然后继续您的代码:

$fetch = mysqli_query($connection, "SELECT * FROM mrb_prprty WHERE `nationalid` = '". $nationalid ."' AND `author` = '" .$username. "'");

while ($row = mysqli_fetch_array($fetch)) {
    (...)
}
$json_mrb = json_encode($return_arr);

覆盖现有文件(或创建一个新文件):

$fh = fopen( $myFile, 'w' );
fwrite( $fh, $json_mrb."\n" );