在PHP中按特定值对JSON文件进行排序,然后保存排序后的JSON文件

在PHP中按特定值对JSON文件进行排序,然后保存排序后的JSON文件,php,json,Php,Json,我对PHP和JSON非常陌生。我目前正在创建一个网站,其中有一个视频游戏排名表。我创建了一个JSON文件,其中包含所有信息: {"id":1,"name":"The Crew","rating":"10","genre":"Racing","release_date":"02.12.2014","developer":"Ivory Tower","publisher":"Ubisoft","platforms":"PS4, Xbox One, PC", "age_restriction":"12

我对PHP和JSON非常陌生。我目前正在创建一个网站,其中有一个视频游戏排名表。我创建了一个JSON文件,其中包含所有信息:

{"id":1,"name":"The Crew","rating":"10","genre":"Racing","release_date":"02.12.2014","developer":"Ivory Tower","publisher":"Ubisoft","platforms":"PS4, Xbox One, PC", "age_restriction":"12"}
{"id":2,"name":"DriveClub","rating":"8","genre":"Racing","release_date":"07.10.2014","developer":"Evolution Studios","publisher":"Sony Interactive Entertainment","platforms":"PS4", "age_restriction":"3"}
{"id":3,"name":"Project CARS","rating":"8","genre":"Racing","release_date":"06.05.2015","developer":"Slightly Mad Studios","publisher":"Slightly Mad Studios","platforms":"PS4, Xbox One, PC", "age_restriction":"3"}
{"id":4,"name":"Project CARS 2","rating":"8","genre":"Racing","release_date":"21.09.2017","developer":"Slightly Mad Studios","publisher":"Slightly Mad Studios","platforms":"PS4, Xbox One, PC", "age_restriction":"3"}
{"id":5,"name":"Dirt Rally","rating":"10","genre":"Racing","release_date":"07.12.2015","developer":"Codemaster","publisher":"Codemaster","platforms":"PS4, Xbox One, PC, MacOs, Linux", "age_restriction":"6"}
{"id":6,"name":"Wreckfest","rating":"8","genre":"Racing","release_date":"15.01.2014","developer":"Bugbear Entertainment","publisher":"THQ Nordic","platforms":"PS4, Xbox One, PC", "age_restriction":"12"}
{"id":7,"name":"Gran Turismo Sport","rating":"8","genre":"Racing","release_date":"17.10.2017","developer":"Polyphony Digitial","publisher":"Polyphony Digital","platforms":"PS4", "age_restriction":"3"}
{"id":8,"name":"F1 2019","rating":"8","genre":"Racing","release_date":"25.06.2019","developer":"Codemaster","publisher":"Codemaster","platforms":"PS4, Xbox One, PC", "age_restriction":"3"}
{"id":9,"name":"Need For Speed Pay Back","rating":"6","genre":"Racing","release_date":"10.11.2017","developer":"Ghost Games","publisher":"EA","platforms":"PS4, Xbox One, PC", "age_restriction":"12"}
{"id":10,"name":"The Crew 2","rating":"7","genre":"Racing","release_date":"31.05.2018","developer":"Ivory Tower","publisher":"Ubisoft","platforms":"PS4, Xbox One, PC", "age_restriction":"12"}
我已经通过使用此代码,通过评级对游戏进行了排序

$data = jsonLoadAllGames("data/json/games.json");
usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a->rating > $b->rating ? -1 : 1; //Compare the scores
});   
但是,我不知道如何将排序列表保存到链接到排名表的JSON文件中。目前,它正在按它们在中列出的顺序显示它们。在另一页上显示游戏需要id。(顺便说一句,我不允许使用SQL)

我希望这是有意义的,如果有任何问题,请随意提问。提前感谢您的帮助

$data = jsonLoadAllGames("data/json/games.json");
usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a->rating > $b->rating ? -1 : 1; //Compare the scores
});

file_put_contents("data/json/mynewfile.json", json_encode($data));
此外,您应该检查file\u put\u内容的返回值以检查错误


如果您需要具有良好缩进的输出文件,请将JSON_PRETTY_PRINT作为JSON_encode的第二个参数。

尝试使用JSON_encode$数据数组,并将输出提供给file_put_contents,以创建一个fileFind,其中定义了
jsonLoadAllGames()
,然后执行它所做的操作,但操作相反。或者,找到首先编写文件的函数并使用它。我们只能猜测,除了“输出看起来像JSON的东西,但不是真的”@sammitch嘿,很抱歉搞混了!“jsonLoadAllGames()”尚未在文件中创建数据。我自己已经把数据写进了JSON。非常感谢,这就成功了!我真的很感谢你的帮助!祝你过得愉快,保持安全:)谢谢,你也是。你能证明答案是正确的吗?