繁体   English   中英

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

[英]Sorting JSON File by specific value in PHP and then saving the sorted JSON file

我对 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_put_contents 的返回值以检查错误。

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

暂无
暂无

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

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