php - Formatting JSON output in wordpress -
i have function queries 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 wp_infokiosksfloors f, wp_infokiosksmap m f.floor_id = m.floor'); // json_encode($args); $map = array(); $args = array(); foreach ($map_rows $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 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 output json similar 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 best of both international , 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 best of both international , 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 best of both international , 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 , 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 , 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 , 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 , 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 , 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 , 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 need 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 location 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 best of both international , 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 , 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 , 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 best of both international , 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 , 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 , nice toilets", "category": "services", "subcategory": "all-toilet", "icon": "../img/map/icon-toilet.svg", "x": "0.4687", "y": "0.5995" } ] } ] }
only first part appears 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 please me how can go this?
you need change $args= [
$args[] = [
.
in case of $args= [
, replace value of $args
every time in loop. in case of $args[] = [
add new item $args
array.
Comments
Post a Comment