用php格式化JSON

用php格式化JSON,php,arrays,json,Php,Arrays,Json,我正在尝试为JavaScript应用程序构建一些JSON输出,它希望以一种非常具体的方式生成该格式 目前,我已经成功地从我的php文件生成了一个JSON输出。我遇到的问题是括号的使用与脚本预期的不同。下面是我如何构建JSON的: // define my feed $feed = [ "mapwidth" => "800", "mapheight" => "600", "categories" => [], "levels" => [

我正在尝试为JavaScript应用程序构建一些JSON输出,它希望以一种非常具体的方式生成该格式

目前,我已经成功地从我的php文件生成了一个JSON输出。我遇到的问题是括号的使用与脚本预期的不同。下面是我如何构建JSON的:

// define my feed
$feed = [
    "mapwidth" => "800",
    "mapheight" => "600",
    "categories" => [],
    "levels" => [
        "id" => "canada",
        "title" => "Canada",
        "map" => "src/images/shops-map.svg",
        "locations"         => []
    ]
];


// grab our items from the database
$locations = perch_collection('Shops', [
    'sort-order'    => 'DESC',
    'skip-template' => true,
    'count'         => 10,
]);

//loop through the items
if (count($locations)) {
    foreach($locations as $location) {
        $feed['levels']['locations'][] = (object)[
            'id'             => $location['id'],
            'title'          => $location['name'],
            'about'   => $location['info'],
            "description" => $location['info'],
            "category" => "clothing",
            "thumbnail" => $location['image'],
            "x" => "0.3781",
            "y" => "0.4296"
        ];
    }
}

//ready to generate
header('Content-Type: application/json');
echo json_encode($feed, JSON_PRETTY_PRINT);
我的输出:

{
    "mapwidth": "800",
    "mapheight": "600",
    "categories": [],
    "levels": {
        "id": "canada",
        "title": "Canada",
        "map": "src\/images\/shops-map.svg",
        "locations": [
            {
                "id": "u-001",
                "title": "Test Shop",
                "about": "sdf sdfsd fsd fsdfdsf sd fsddsfsdf sdfsdfdsfsdf",
                "description": "sdf sdfsd fsd fsdfdsf sd fsddsfsdf sdfsdfdsfsdf",
                "category": "clothing",
                "thumbnail": null,
                "x": "0.3781",
                "y": "0.4296"
            }
        ]
    }
}
期望输出:

{
    "mapwidth": "800",
    "mapheight": "600",
    "categories": [],
    "levels": [
        {
            "id": "canada",
            "title": "Canada",
            "map": "src\/images\/shops-map.svg",
            "locations": [
                {
                    "id": "u-001",
                    "title": "Test Shop",
                    "about": "sdf sdfsd fsd fsdfdsf sd fsddsfsdf sdfsdfdsfsdf",
                    "description": "sdf sdfsd fsd fsdfdsf sd fsddsfsdf sdfsdfdsfsdf",
                    "category": "clothing",
                    "thumbnail": null,
                    "x": "0.3781",
                    "y": "0.4296"
                }
            ]
        }
    ]
}

请注意级别:{是输出的。我的脚本希望看到方括号后面跟着花括号。我对这一点失去了理智。我认为这是在只有一个项的情况下它处理数组的方式,尽管这还没有让我找到解决方案。

您需要在两个位置添加额外的数组级别

"levels" => [[
    "id" => "canada",
    "title" => "Canada",
    "map" => "src/images/shops-map.svg",
    "locations"         => []
]]
当您向标高添加位置时

$feed['levels'][0]['locations'][]

它需要比您多一个级别的数组。$feed['levels']=[['id'=>…]]