php - 如何将表单数据添加到 JSON 文件?

标签 php json url

现在我有这个表格:

<form name='steampaypalinfo' action='profile.php' method='post'>
PayPal Email: <br><input type='email' name='paypal'><br>
Steam Trade URL: <br><input type='text' name='tradeurl'><br><br>
<input type='submit' value='Update' name='submit'>
</form>

我正在像这样在 PHP 中检索数据:

if (isset($_POST['submit'])) {
if (empty($_POST['paypal'])) {
    $paypalerror = "PayPal email is required!";
} else {
    $paypalemail = $_POST['paypal'];
}

if (empty($_POST['tradeurl'])) {
$tradeurlerror = "Steam Trade URL is required!";
} else {
    $tradeurl = $_POST['tradeurl'];
}

如您所见,我将表单数据(电子邮件和链接)存储到两个名为 paypalemail 和 tradeurl 的变量中。

现在我想将此数据添加到我已经创建的 JSON 文件中。 JSON 文件现在看起来像这样:

{
"response": {
    "players": [
        {
            "steamid": "76561198064105349",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "PUGLORD",
            "lastlogoff": 1445136051,
            "commentpermission": 2,
            "profileurl": "http://steamcommunity.com/id/ashland3000/",
            "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65.jpg",
            "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_medium.jpg",
            "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_full.jpg",
            "personastate": 0,
            "realname": "Jakob",
            "primaryclanid": "103582791439857810",
            "timecreated": 1337817157,
            "personastateflags": 0,
            "loccountrycode": "US",
            "locstatecode": "NY"
        }
    ]

}
}

我希望数据 paypalemail 和 tradeurl 数据进入玩家数组。我阅读了有关使用 file_put_contents 或 fwrite 的信息,但似乎都不起作用。

问题:如何将 PayPal 和 Steam URL 数据添加到已创建的 JSON 文件中?如何将该数据添加到 JSON 文件中的已创建数据并正确设置格式?

谢谢,任何帮助都会很棒!

编辑 我试过这个:

$file = file_get_contents("cache/players/{$steam64}.json");
$json = json_decode($file, true);
$player_array['paypalemail'] = $paypalemail;

$json = json_encode($player_array);
$file = fopen("cache/players/{$steam64}.json", 'w');
fwrite($file, $json);
fclose($file);

它可以工作,但它会覆盖 JSON 文件中的数据。如何添加而不覆盖它?

最佳答案

应该是这样的:

$file = file_get_contents('players.json');
$json = json_decode($file, true); //second parameter, return as associative array
$player_array = &$json['response']['players'][0];
//lets setup new keys
$player_array['paypalemail'] = "johanroure@cool.com";
//whatever you want to add
$new_json = json_encode($json,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); //pretty and without slashes
$file = fopen('players.json', 'w'); //w set pointer to beginning and truncate to 0 :D
fwrite($file, $new_json);
fclose($file);

关于php - 如何将表单数据添加到 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200091/

相关文章:

php - jQuery 和 ajax 在 jQuery UI 对话框小部件中更新 MySQL 中的行

php - .htaccess 只出现 html 而没有 CSS 或 PHP

如果 session.save_handler 设置为内存缓存,Phpmyadmin 不起作用

json - Facebook Applinks - 在 al_applink_data 中传递自定义 JSON 数据

java - 如何在 Java 中使用 servlet 过滤器来更改传入的 servlet 请求 url?

java.security.AccessControlException : access denied (java. lang.RuntimePermission createSecurityManager)

php - Opencart - 添加产品出现错误

java - 解析 Json 数组导致 This is not a JSON Array 异常

javascript - 如何获取body.data[0].images.fixed_height.url的值?

javascript - 在超链接中给出链接 URL 时无法打开,但将 URL 直接粘贴到地址栏中时打开