我使用sql查询在codeigniter中生成一个sql字符串。
$result['tc'] = $this->db->query("select * from stationList");
然后我将命令传递给视图:
$this->load->view('jsontc',$result);
然后使用foreach循环将其更改为数组并将其编码为json:
$emparray = array();
foreach ($tc as $row)
{
$emparray[] = $row;
}
$page = json_encode(array('stationList' => $emparray));
echo $page.
所以我得到以下结果
{
"stationList":[
{
"no":"1",
"location":"Temple Mall North",
"districtL":"Kowloon",
"districtS":"Wong Tai Sin",
"address":"Temple Mall North Carpark, Level 3,\n136 Lung Cheung Rd, Wong Tai Sin, Kln",
"provider":"CLP",
"parkingNo":"320-322",
"img":"\/EV\/PublishingImages\/common\/map\/map_thumb\/Entrance_Lung%20Cheung.jpg",
"lat":"22.3425903320313",
"lng":"114.190719604492",
"type":"SemiQuick"
},
但如何为每组记录添加标题,如下所示?
{
"stationList":[
station{
"no":"1",
"location":"Temple Mall North",
"districtL":"Kowloon",
"districtS":"Wong Tai Sin",
"address":"Temple Mall North Carpark, Level 3,\n136 Lung Cheung Rd, Wong Tai Sin, Kln",
"provider":"CLP",
"parkingNo":"320-322",
"img":"\/EV\/PublishingImages\/common\/map\/map_thumb\/Entrance_Lung%20Cheung.jpg",
"lat":"22.3425903320313",
"lng":"114.190719604492",
"type":"SemiQuick"
},
station{...
答案 0 :(得分:0)
为您的密钥选择唯一标识符非常重要,因此我选择了no
您可以尝试:
foreach ($tc as $row)
{
$emparray["{$row['no']}"] = $row;
}