使用PHP编辑JSON对象中的JSON对象
如何在这个JSON文件中增加x或y?我发布了我的PHP代码以增加计数器,但我不知道如何访问“infos”->“x”或“y” JSON文件使用PHP编辑JSON对象中的JSON对象,php,html,json,Php,Html,Json,如何在这个JSON文件中增加x或y?我发布了我的PHP代码以增加计数器,但我不知道如何访问“infos”->“x”或“y” JSON文件 { "counter": 4, "infos": { "x": 1, "y": 2 } } PHP文件 <?php $contents = file_get_contents('../test.json'); $contentsDecoded = json_decode($contents, true); $content
{
"counter": 4,
"infos": {
"x": 1,
"y": 2
}
}
PHP文件
<?php
$contents = file_get_contents('../test.json');
$contentsDecoded = json_decode($contents, true);
$contentsDecoded['counter']++;
$json = json_encode($contentsDecoded);
file_put_contents('../test.json', $json);
?>
只需使用
$decoded['infos']['x']++;
$contentsDecoded['infos']['x']+
?有时它非常简单。。。。很有效谢谢