gpt4 book ai didi

javascript - 如何使用 Php 在 php 中附加 Json 文件

转载 作者:行者123 更新时间:2023-12-01 01:39:19 27 4
gpt4 key购买 nike

我在 php 文件中嵌入了一个 json 文件,我想从 json 文件中的表单添加新信息...

我的表格是

<form action="process.php" method="POST">
First name:<br>
<input type="text" name="firstName">
<br><br/>
Last name:<br>
<input type="text" name="lastName">
<br><br>

Email:<br>
<input type="text" name="email">
<br><br>

Mobile:<br>
<input type="text" name="mobile">
<br><br>


<input type="submit" value="Submit">
</form>

php 文件是

<?php

$myFile = "data.php";
$arr_data = array(); // create empty array

try
{
//Get form data
$formdata = array(
'firstName'=> $_POST['firstName'],
'lastName'=> $_POST['lastName'],
'email'=>$_POST['email'],
'mobile'=> $_POST['mobile']
);

//Get data from existing json file
$jsondata = file_get_contents($myFile);

// converts json data into array
$arr_data = json_decode($jsondata, true);

// Push user data to array
array_push($arr_data,$formdata);

//Convert updated array to JSON
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

//write json data into data.json file
if(file_put_contents($myFile, $jsondata)) {
echo 'Data successfully saved';
}
else
echo "error";

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

?>

当我单击提交按钮时,新数据将如下添加

{
"heroes": [
{
"firstName": "Vijay",
"lastName": "reddy",
"email": "asdv@ymail.com",
"mobile": ""
},
{
"firstName": "Paki",
"lastName": "Webb",
"email": "vahodid@yahoo.com",
"mobile": "66464646464"
},
{
"firstName": "sagar",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98989898"
}
],
"0": {
"firstName": "sagar",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98989898"
},
"1": {
"firstName": "Purnima",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98652845"
}
}

但我想要数据为

{
"heroes": [
{
"firstName": "Vijay",
"lastName": "reddy",
"email": "asdv@ymail.com",
"mobile": ""
},
{
"firstName": "Paki",
"lastName": "Webb",
"email": "vahodid@yahoo.com",
"mobile": "66464646464"
},
{
"firstName": "sagar",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98989898"
},
{
"firstName": "sagar",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98989898"
},
{
"firstName": "Purnima",
"lastName": "rawal",
"email": "searchbbc1881@gmail.com",
"mobile": "98652845"
}
]
}

即删除索引并将新数据与其他数据一起写入 json 中...到目前为止我无法做到...所以请帮助我...提前致谢。

最佳答案

您刚刚对整个 json 数组执行了 array_push,您希望它位于“heroes”内

// converts json data into array
$arr_data = json_decode($jsondata, true);
$arr_data['heroes'][] = $formdata; // Will push $formdata inside 'heroes' array from json

如果您更喜欢使用 array_push,请执行以下操作:

// converts json data into array
$arr_data = json_decode($jsondata, true);
array_push($arr_data['heroes'], $formdata); // Will push $formdata inside 'heroes' array from json

关于javascript - 如何使用 Php 在 php 中附加 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52559500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com