小编Sam*_*han的帖子

PHP JSON_encode无法正常工作

我想从this获取 JSON输出。不幸的是json_encode()函数不会将数组编码为该格式。一切都没有回报。这是我的代码。

$output = array(
    'responseData' => array(),
    'responseDetails' => null,
    'responseStatus' => 200
);

$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
    foreach ($row as $k => $v) {
        $output['responseData']['result'][$x][$k] = $v;
    }
    $x++;
}

print_r($output);
header('Content-Type: application/json');
echo json_encode($output , JSON_FORCE_OBJECT);
Run Code Online (Sandbox Code Playgroud)

我找不到原因。有人请帮助我找到解决方案。

编辑:对不起。这是输出-

预期的JSON输出-

{
"responseData": {
    "results": [{
        "qid": 1,
        "qtitle": "When do we finish this project ?",
        "qimage_url": "http://www.wearesliit.com/example.png",
        "user": "samith",
        "date": "2016-01-01T02:15:12.356Z",
        "type": 1,
        "category": 5,
        "tags": ["common_senese", "truth", "bazsa_awsanna"],
        "note": "Sample …
Run Code Online (Sandbox Code Playgroud)

javascript php api json

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

api ×1

javascript ×1

json ×1

php ×1