【发布时间】:2015-03-01 03:47:49
【问题描述】:
我环顾四周,找到了函数file_get_contents 和file_put_contents,并尝试编写一个基本代码,将我的代码中[0]['Face'] 的Name 更改为“Testing Face”,但它完全覆盖了JSON .
这是我在 PHP 代码之前的 JSON:
[{"Hat":{"Name":"Stylin' Shades","Id":"221177193"},{"Gear":{"Name":"Red Sparkle Time Claymore", "Id":"221181437"}}, {"Face":{"Name":"Joyful Smile", "Id":"209995366"}]
应该改成
[{"Hat":{"Name":"Stylin' Shades","Id":"221177193"},{"Gear":{"Name":"Red Sparkle Time Claymore", "Id":"221181437"}}, {"Face":{"Name":"Testing Face", "Id":"209995366"}]
但是,整个 JSON 被替换为 [{"Face":{"Name":"No"}}]
我的 PHP:
<?php
$file = 'notifier.json';
$jsonString = file_get_contents($file);
$data = json_decode($jsonString);
$data[0]['Face']['Name'] = 'Testing Face';
$newJSON = json_encode($data);
file_put_contents($file, $newJSON);
?>
谢谢!
【问题讨论】:
标签: php arrays json file object