我试图将值添加到文件中但是我第一次使用PHP而我找不到该怎么办?
目标是将处方集(add-name和add-link ID)中的值添加到具有相同结构的JSON文件中并保存。 (不要暂时将它保存在文件中)。 我要求以密钥为例: { "名称":"谷歌&#34 ;, " URL":" google.es }
<!DOCTYPE html>
<html>
<head>
<title>SSL Checker</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript" src="js/json.json" charset="utf-8"></script>
</head>
<body onLoad="start()">
<div id="title">
<h1>SSL Checker</h1>
</div>
<div id="data">
<form action="javascript:void(0);" method="POST" onsubmit="SSL.Add()">
<input type="text" id="add-name" placeholder="Name"></input>
<input type="text" id="add-link" placeholder="Link"></input>
<input type="submit" value="Add">
</form>
<div id="edit" role="aria-hidden">
<form action="javascript:void(0);" method="POST" id="saveEdit">
<input type="text" id="edit-name">
<input type="submit" value="Edit" /> <a onclick="CloseInput()" aria-label="Close">✖</a><br>
<input type="text" id="edit-name1">
</form>
</div>
<p id="counter"></p>
</div>
<div id="table">
<table style="overflow-x:auto;">
<tr>
<th>Sites:</th>
</tr>
<tbody id="urls">
</tbody>
</table>
</div>
</body>
</html>
JSON:
var Checker = [{
name:"Google",
url: "google.es",
},
{
name:"Yahoo",
url: "yahoo.com",
}
]
答案 0 :(得分:0)
<?php
$checkers = []; // assign an empty array to the variable $checkers;
// cast associative array to an object and add to the checker array
$checkers[] = (object)['name' => 'Google', 'url' => 'google.es'];
// create an object using the predefined stdClass
$item = new stdClass();
$item->name = 'Yahoo';
$item->url = 'yahoo.com';
$checkers[] = $item;
// ideally create your own class representing the checker object
echo json_encode($checkers);
输出:
[{
"name": "Google",
"url": "google.es"
}, {
"name": "Yahoo",
"url": "yahoo.com"
}]
请参阅此链接以获取更多信息:In PHP, how can I add an object element to an array?