Php 在wordpress中格式化JSON输出

Php 在wordpress中格式化JSON输出,php,json,wordpress,Php,Json,Wordpress,我有一个查询wordpress数据库表的函数: public function generateJson() { if (isset($_POST['update_json'])) { global $wpdb; $map_rows = $wpdb->get_results( 'select f.floor_id, f.floor_title,

我有一个查询wordpress数据库表的函数:

    public function generateJson() {
        if (isset($_POST['update_json'])) {

            global $wpdb;
            $map_rows = $wpdb->get_results(
                    'select f.floor_id, f.floor_title,
                        f.floor_map,m.id,m.type,m.title,
                        m.about,m.description,m.icon,m.link,
                        m.xcord,m.ycord,m.type,m.link
                        from wp_infokiosksfloors f, 
                        wp_infokiosksmap m 
                        where f.floor_id = m.floor');

            // json_encode($args);

            $map = array();
            $args = array();
            foreach ($map_rows as $locations => $location) {
                //$map[] = $value->id;

                      $args= [
                          'mapwidth' => '800',
                          'mapheight' => '600',
                    'level' => array([
                            'id' =>$location->floor_id,
                            'title' => $location->floor_title,
                            'map' => $location->floor_map,
                            "locations" => array([
                                    'id' => $location->id,
                                    'title' => $location->title,
                                    'about' => $location->about,
                                    'description' => $location->description,
                                    'subcategory' =>$location->type,
                                    'link' => $location->link,
                                    'icon' => $location->icon,
                                    'x' => $location->xcord,
                                    'y' => $location->ycord
                                ])
                        ])
                ];



          echo json_encode($args, JSON_PRETTY_PRINT.JSON_UNESCAPED_SLASHES);

            }


            echo "<h4 style='color:red'>Map locations successfully updated</h4>";
        }
        $fp = '../wp-content/themes/xxx/js/map/infomap.json';
        return   file_put_contents($fp, json_encode($args, JSON_PRETTY_PRINT.JSON_UNESCAPED_SLASHES));
    }
只有第一部分显示如下:

{
    "mapwidth": "800",
    "mapheight": "600",
    "level": [{
            "id": "Second",
            "title": "Second Floor",
            "map": wp-content/uploads/2014/08/hotel.jpg",
            "locations": [{
                    "id": 1,
                    "title": "My Barber",
                    "about": "Barber shop",
                    "description": "Barber shop",
                    "subcategory": "ATMs",
                    "link": "",
                    "icon": "wp-content/uploads/2014/08/hotel.jpg",
                    "x": "54.75",
                    "y": "64.55"
                }]
        }]
}

有人能帮我怎么做吗?

您需要将
$args=[
更改为
$args[]=[

$args=[
的情况下,每次在循环中替换
$args
的值。在
$args[]=[
的情况下,向
$args
数组添加一个新项

{
    "mapwidth": "800",
    "mapheight": "600",
    "level": [{
            "id": "Second",
            "title": "Second Floor",
            "map": wp-content/uploads/2014/08/hotel.jpg",
            "locations": [{
                    "id": 1,
                    "title": "My Barber",
                    "about": "Barber shop",
                    "description": "Barber shop",
                    "subcategory": "ATMs",
                    "link": "",
                    "icon": "wp-content/uploads/2014/08/hotel.jpg",
                    "x": "54.75",
                    "y": "64.55"
                }]
        }]
}