1

I have this function that queries a wordpress database table:

    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));
    }

I want it to output a json similar to the following format:

{
    "mapwidth": "800",
    "mapheight": "600",
    "levels": [
        {
            "id": "second",
            "title": "Second Floor",
            "map": "../img/map/02-plain.svg",
            "minimap": "images/mall/mall-basement-small.jpg",
            "locations": [
                {
                    "id": "gateA",
                    "title": "Gate 15",
                    "about": "Gate 15",
                    "description": "Gate 15",
                    "category": "flights",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "link":"",
                    "x": "0.6342",
                    "y": "0.4433"
                },
                {
                    "id": "gateb",
                    "title": "Gate 16",
                    "about": "Gate 16",
                    "description": "Gate 16",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.5273",
                    "y": "0.5323"
                },
                {
                    "id": "gatec",
                    "title": "Gate 18",
                    "about": "Gate 18",
                    "description": "Gate 18",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.3923",
                    "y": "0.6170"
                },
                {
                    "id": "gated",
                    "title": "Gate 19",
                    "about": "Gate 19",
                    "description": "Gate 19",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.3720",
                    "y": "0.6807"
                },
                {
                    "id": "gatee",
                    "title": "Gate 20",
                    "about": "Gate 20",
                    "description": "Gate 20",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.4117",
                    "y": "0.7462"
                },
                {
                    "id": "gatef",
                    "title": "Gate 21",
                    "about": "Gate 21",
                    "description": "Gate 21",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.5547",
                    "y": "0.5846"
                },
                {
                    "id": "gateg",
                    "title": "Gate 22",
                    "about": "Gate 22",
                    "description": "Gate 22",
                    "category": "gate",
                    "subcategory": "allgate",
                    "icon": "../img/map/icon-flight-departures.svg",
                    "x": "0.6190",
                    "y": "0.5272"
                },
                {
                    "id": "jcafe",
                    "title": "Java Cafe",
                    "about": "Luke's Java Cafe",
                    "description": "Has the best of both international and local coffee",
                    "category": "restaurant",
                    "subcategory": "all-restaurant",
                    "icon": "../img/map/icon-leisure-bar.svg",
                    "link": "",
                    "x": "0.7079",
                    "y": "0.4009"
                },
                {
                    "id": "jcafe2",
                    "title": "Java Cafe",
                    "about": "Luke's Java Cafe",
                    "description": "Has the best of both international and local coffee",
                    "category": "restaurant",
                    "subcategory": "all-restaurant",
                    "icon": "../img/map/icon-leisure-bar.svg",
                    "link": "",
                    "x": "0.4794",
                    "y": "0.6521"
                },
                {
                    "id": "jcafe3",
                    "title": "Java Cafe",
                    "about": "Luke's Java Cafe",
                    "description": "Has the best of both international and local coffee",
                    "category": "restaurant",
                    "subcategory": "all-restaurant",
                    "icon": "../img/map/icon-leisure-bar.svg",
                    "link": "",
                    "x": "0.6775",
                    "y": "0.2986"
                },
                {
                    "id": "toilet",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.6680",
                    "y": "0.2049"
                },
                {
                    "id": "toilet1",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.6572",
                    "y": "0.4097"
                },
                {
                    "id": "toilet2",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                      "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.8553",
                    "y": "0.5480"
                },
                {
                    "id": "toilet3",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                      "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.8851",
                    "y": "0.5862"
                },
                {
                    "id": "toilet5",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                      "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.6562",
                    "y": "0.5049"
                },
                 {
                    "id": "toilet6",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.4042",
                    "y": "0.6744"
                },
                 {
                    "id": "infodesk",
                    "title": "Information Desk",
                    "about": "Enquiries",
                    "description": "Ask all you need to know",
                    "category": "services",
                    "icon": "../img/map/icon-infocenter.svg",
                    "x": "0.7312",
                    "y": "0.3607"
                },
                 {
                    "id": "counter1",
                    "title": "Counter",
                    "about": "Counter",
                    "description": "Counter",
                    "category": "services",
                    "icon": "../img/map/icon-counter-bpass.svg",
                    "x": "0.5808",
                    "y": "0.5613"
                },
                 {
                    "id": "counter2",
                    "title": "Counter",
                    "about": "Counter",
                    "description": "Counter",
                    "category": "services",
                    "icon": "../img/map/icon-counter-bpass.svg",
                    "x": "0.5228",
                    "y": "0.6109"
                },
                 {
                    "id": "counter3",
                    "title": "Counter",
                    "about": "Counter",
                    "description": "Counter",
                    "category": "services",
                    "icon": "../img/map/icon-counter-bpass.svg",
                    "x": "0.4488",
                    "y": "0.6800"
                }
            ]
        },
        {
            "id": "third",
            "title": "Third Floor",
            "map": "../img/map/03-plain.svg",
            "minimap": "images/mall/mall-basement-small.jpg",
            "locations": [
                {
                    "id": "viplounge",
                    "title": "Vip Lounge",
                    "about": "VIP Lounge: Restricted area",
                    "description": "Entry to this location is under restriction",
                    "category": "restaurant",
                    "icon": "../img/map/icon-viplounge.svg",
                    "link": "",
                    "x": "0.4484",
                    "y": "0.7046"
                },
                 {
                    "id": "jcafe4",
                    "title": "Java Cafe",
                    "about": "Luke's Java Cafe",
                    "description": "Has the best of both international and local coffee",
                    "category": "restaurant",
                    "subcategory": "all-restaurant",
                    "icon": "../img/map/icon-leisure-bar.svg",
                    "link": "",
                    "x": "0.6775",
                    "y": "0.2986"
                },
                {
                    "id": "toilet9",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.7252",
                    "y": "0.4460"
                },
                {
                    "id": "toilet10",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.4699",
                    "y": "0.5975"
                }
            ]
        },
        {
            "id": "first",
            "title": "First Floor",
            "map": "../img/map/01-plain.svg",
            "minimap": "images/mall/mall-first-small.jpg",
            "locations": [
                {
                    "id": "shopfirst",
                    "title": "Darwy Shopping Collection",
                    "about": "All kinds of shopping",
                    "description": "Find ",
                    "category": "restaurant",
                    "icon": "../img/map/icon-leisure-shop.svg",
                    "link": "",
                    "x": "0.5402",
                    "y": "0.5561"
                },
                 {
                    "id": "jcafe5",
                    "title": "Java Cafe",
                    "about": "Luke's Java Cafe",
                    "description": "Has the best of both international and local coffee",
                    "category": "restaurant",
                    "subcategory": "all-restaurant",
                    "icon": "../img/map/icon-leisure-bar.svg",
                    "link": "",
                    "x": "0.4090",
                    "y": "0.7126"
                },
                {
                    "id": "toilet7",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.8863",
                    "y": "0.4737"
                },
                {
                    "id": "toilet8",
                    "title": "Toilets",
                    "about": "Toilets, Washrooms",
                    "description": "Clean and nice toilets",
                    "category": "services",
                    "subcategory": "all-toilet",
                    "icon": "../img/map/icon-toilet.svg",
                    "x": "0.4687",
                    "y": "0.5995"
                }
            ]
        }
    ]
}

Only the first part appears as follows:

{
    "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"
                }]
        }]
}

Could someone please help me how I can go about this?

0

1 Answer 1

2

You need to change $args= [ to $args[] = [.

In case of $args= [, you replace the value of $args every time in the loop. In case of $args[] = [ you add a new item to the $args array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.