I want to retrieve data in json format given below using php. Code for the same is written as I tried a lot but not able to do it. Please Help Me :
<?php
$db=new PDO('mysql:dbname=punehack;host=localhost;','*******','********');
$row=$db->prepare('select * from gyms where live="yes" order by rand() LIMIT 2');
$row->execute();//execute the query
$json_data=array();//create the array
$json_latdata=array();
$result = array();
$result1 = array();
foreach($row as $rec)//foreach loop
{
$json_array['id']=$rec['id'];
$json_array['username']=$rec['username'];
$json_array['images']=$rec['images'];
$json_array['name']=$rec['name'];
$json_array['location']=$rec['location'];
$json_array['category1']=$rec['category1'];
$json_array['category2']=$rec['category2'];
$json_array['category3']=$rec['category3'];
$json_array['city']=$rec['city'];
$json_array['type']=$rec['type'];
$json_array['male']=$rec['male'];
$json_array['female']=$rec['female'];
$json_array['fitdiary']=$rec['fitdiary'];
$json_array1['latitude']=$rec['latitude'];
$json_array1['longitude']=$rec['longitude'];
array_push($json_data,$json_array);
array_push($json_latdata,$json_array1);
}
$result = $json_data;
$result1 = $json_latdata;
echo json_encode(array($result,$result1));
?>
Output for this is :
[
[{
"id": "101",
"username": "thefitnessfloor-shivajinagar",
"images": "thefitnessfloor\/shivajinagar\/ambiance\/1.jpg",
"name": "The Fitness Floor",
"location": "Shivaji Nagar",
"city": "Pune",
"type": "Gym",
"male": "yes",
"female": "yes",
"fitdiary": "yes"
}, {
"id": "97",
"username": "bodyworks-wanowrie",
"images": "bodyworks\/wanowrie\/ambiance\/1.jpg",
"name": "Body Works",
"location": "Wanowrie",
"city": "Pune",
"type": "Gym",
"male": "yes",
"female": "yes",
"fitdiary": "yes"
}],
[{
"latitude": "18.526776",
"longitude": "73.843531"
}, {
"latitude": "18.481161",
"longitude": "73.901035"
}]
]
Need to retrieve the data in the below given format :
{
"id": 1,
"name": "The Flying Falafel",
"username": "jhhkjhkhkh",
"images": "bashfjdskjfksjdkj",
"categories": [
"category1",
"category2",
"category3"
],
"location": "1051 Market St, SoMa, San Francisco, CA 94103",
"city": "hjhjhjhjh",
"type": "hjhjhjhjh",
"male": "y",
"female": "y",
"coordinate": {
"latitude": 37.78125,
"longitude": -122.4113007
}
}