简体   繁体   English

使用PHP读写JSON

[英]Using PHP to read and write to JSON

So currently I'm working on a project that requires the use of PHP and JSON. 所以目前我正在开发一个需要使用PHP和JSON的项目。 The way I have currently set up my code is so that I read the JSON file in using file_get_contents and then use the built in json_decode() function. 我当前设置代码的方式是使用file_get_contents读取JSON文件,然后使用内置的json_decode()函数。 Everything is reading perfectly, it's the writing part that is giving me trouble. 一切都在完美阅读,而写作部分却给我带来麻烦。

The way I have my JSON tree set up is: 我设置JSON树的方式是:

{
"test":[
        {
        "testName":"Winter 2011",
        "testDate":"12/04/2011",
        "testTime":"2:00pm",
        "testRoom":"room",
        "cap": 10,
        "spotsLeft": 0,
        "student":[
            {
                "fname":"Bob",
                "lname":"Roberts",
                "usrnm":"bobr",
                "email":"bob@gmail.com"
            }
        ]
    }
]
}

To get the right test (since there will be multiple tests) I'm using a foreach loop and then I make sure there isn't a duplicate. 为了得到正确的测试(因为将有多个测试)我正在使用foreach loop ,然后我确保没有重复。 At this point I'm ready to add a new student to the JSON file so I call: ` 此时我已经准备好将新学生添加到JSON文件中,所以我打电话给:`

$file = file_get_contents('testDates.json');
$json = json_decode($file, true);

//Go through each test in the file
foreach ($json[test] as $t){
   .....
   //Works if I only do $t[student] but that only adds it to the $t[] and not the
   //JSON file. Can't seem to get it to add to the whole JSON string.
   array_push($json[$t][student], $newStudent);
   echo json_encode($json);
   .....
   //Use a file_put_contents later to write the file out

Any thoughts would be extremely helpful. 任何想法都会非常有帮助。 It's probably a pointer error to a certain point int he array but a second set of eyes never hurt. 这可能是指向数组中某个点的指针错误,但第二组眼睛从未受伤。 Thanks. 谢谢。

$t is a copy, it is not referencing original object. $t是一个副本,它没有引用原始对象。 This appens with every foreach cycle. 这适用于每个foreach循环。 Use instead: 改为使用:

foreach ($json[test] as $k => $t){
    array_push($json[test][$k][student], $newStudent);
    ...
}

Or you can try: 或者您可以尝试:

foreach ($json[test] as &$t){
    array_push($t[student], $newStudent);
    ...
}

See here to learn how foreach works: http://php.net/manual/en/control-structures.foreach.php 参见此处以了解foreach的工作原理: http//php.net/manual/zh/control-structures.foreach.php

You're using wrong $json[$t] inside foreach instead of $json["test"][$t] and also you need to use key-value pairs 你在foreach中使用了错误的$ json [$ t]而不是$ json [“test”] [$ t],你还需要使用键值对

$testVal = $json["test"];
foreach ($testVal as $key => $val){
    . . .
    array_push($testVal[$key]["student"], $newStudent);
    . . .
}

PS use $json['test'] instead of $json[test], the first one causes syntax warning which PHP interprets as string "test" (in your case it's working:) ) PS使用$ json ['test']而不是$ json [test],第一个引发语法警告,PHP将其解释为字符串“ test”(在您的情况下,它是有效的:))

Try this: 尝试这个:

$json = json_decode($jsonString);

if($json["color"] == "blue"){
 ...
}

$json["language"] = "php";

$newJsonString = json_encode($json);

Isn't that easy? 这不容易吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM