【发布时间】:2017-12-30 18:18:23
【问题描述】:
首先,这是我正在使用的代码
$json = file_get_contents('./releases.json');
$data = json_decode($json, TRUE);
$region = isset($_GET['region']) ? $_GET['region'] : null;
# if region is not null: ?region=8
if ($region) {
$region_filter = function($v) use ($region) {
// 8 == Worldwide
if ($v['region'] == $region || $v['region'] == 8) {
return true;
} else {
return false;
}
};
$data = array_filter($data['data'], $region_filter);
}
header('Content-Type: application/json');
echo json_encode(array('data' => $data));
如您所见,我使用 array('data' => $data) 将我的 JSON 对象 ($data) 存储在名为 'data' 的数组中,但所做的只是创建一个包含所有其他对象不是数组,我该如何解决?非常感谢!
releases.json 示例:
{
"last_update_on": "2017-12-30",
"data": [{..}, {..}]
}
【问题讨论】:
-
我不明白这个问题。你期望的输出是什么? release.json 的结构是什么?
-
抱歉,我编辑了我的问题
-
假设你想要,
echo json_encode($data);,否则显示你的预期输出。 -
是的,但是您想如何解决这个问题?您期望什么结构?
-
我想要的是 {"data":[..]} 以及我使用 ?region= 获得的新过滤数据