php - PHP 将多个对象放入 json 文件
问题描述
所以我一直在研究这个 PHP API,它会将 JSON 保存到 PHP 文件中。该代码有效,但只允许 1 个对象。如何在不删除旧 JSON 的情况下添加另一个对象(附加 JSON)
<?php
//-- Parameters --\\
$id = $_GET['id'];
$language = $_GET['language'];
$filter = $_GET['filter'];
$commands = $_GET['commands'];
$name = $_GET['name'];
$members = $_GET['members'];
//-- Read and Parse JSON --\\
$string = file_get_contents("./Data/Guilds.json");
$json_a = json_decode($string, true);
if(isset($json_a[$id])) {
}
else {
//-- Initialize Array --\\
$obj[$id] = Array();
//-- Set Array Objects --\\
$obj[$id]["language"] = $language;
$obj[$id]["filter"] = $filter;
$obj[$id]["commands"] = $commands;
$obj[$id]["information"]["name"] = $name;
$obj[$id]["information"]["members"] = $members;
//-- Encode and Write --\\
$fp = fopen('./Data/Guilds.json', 'w+');
fwrite($fp, $json);
fclose($fp);
}
解决方案
推荐阅读
- ios - Ionic 3,IOS,应用内购买
- java - 元数据提取器类文件无法在 java 类中导入
- algorithm - 用一个常数因子确定嵌套循环的时间和空间复杂度
- classification - KNIME - 多个属性上的字符串到文档节点
- google-maps - Google Places API - 地点详细信息查找的“place_id”结果不正确
- swagger-editor - 为什么 Swagger Editor 不发送 Cookie 标头?
- java - FileDescriptor.in 与 System.in
- android - mapping.txt 中的数字部分是什么
- java - 通过 selenium 在 paytm 主页上发起搜索的问题
- java - when an item in an out of the 2 spinner view is selected, other should be auto filled. 所有这些仅使用 assets 文件夹中的本地 json 文件