如何使用PHP编辑特定的JSON键值 [英] How to edit specific JSON key values using PHP
问题描述
我有一个带有JSON对象的JSON文件,我正在尝试使用PHP进行读取和编辑,但是我想更改特定的Key值,事实证明这有点问题.任何人都知道他们可能会有所帮助的任何指针或链接吗?
I have a JSON file with a JSON object I am attempting to read and edit using PHP but I wanted to change specific Key values which is proving to be abit of a problem. Would anyone have any pointers or links they may know that may help?
谢谢
推荐答案
您可以尝试一下.
首先,解码您的JSON:
Firstly, decode your JSON:
$json_object = file_get_contents('some_file_name.json');
$data = json_decode($json_object, true);
然后编辑所需内容,例如:
Then edit what you want such as:
$data['some_key'] = "some_value";
最后将其重新写到文件(或更新的文件)上:
Finally rewrite it back on the file (or a newer one):
$json_object = json_encode($data);
file_put_contents('some_file_name.json', $json_object);
注意:我假设JSON来自文件,但是您可以很好地使用返回JSON对象的任何东西来代替该文件系统功能.
Note: I assumed that the JSON comes from a file, but in place of that file system function you can very well use anything that returns a JSON object.
这篇关于如何使用PHP编辑特定的JSON键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!