从JSON加载模块的详细信息

时间:2014-07-01 02:39:32

标签: php json

我有一个应该从json文件加载模块的函数。这是功能:

$json = file_get_contents("modules.json");
$modules =json_decode($json, true);
foreach ($modules as $module) {
    include('include/scripts/' . $module);
}

这是JSON:

["gallery.class.php", "blog.class.php", "members.class.php"]

此功能有效,但我希望它有更多细节,例如天气是否启用,以及描述等等。

1 个答案:

答案 0 :(得分:0)

您最好的选择可能是将json设置为数组 [] 或对象 {}

看起来像这样:

[
    {
        "name": "gallery",
        "description": "DESCRIPTION",
        "link": "gallery.class.php"
    },
    {
        "name": "Blog",
        "description": "DESCRIPTION",
        "link": "blog.class.php"
    },
    {
        "name": "Members",
        "description": "DESCRIPTION",
        "link": "members.class.php"
    }
]

在您的PHP代码中,您只需将json数据作为项目循环

$json = file_get_contents("modules.json");
$modules =json_decode($json, true);
foreach ($modules as $module) {
    include('include/scripts/' . $module['link']);
}