简体   繁体   中英

How to add title to json format output in codeigniter

I have used a sql query to generate a sql string in codeigniter.

    $result['tc'] = $this->db->query("select * from stationList");

then I pass it to the view ith the command:

    $this->load->view('jsontc',$result);

then using foreach loop to change it into array and encode it to json:

    $emparray = array();

foreach ($tc as $row)
{

$emparray[] = $row;

}

$page = json_encode(array('stationList' => $emparray));
echo $page.

so i get the following result

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

but how can I add a title to each set of record like the following?

{
  "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{...

It's important to choose a unique identifier for your key so i have chosen the no You can try :

foreach ($tc as $row)
{
   $emparray["{$row['no']}"] = $row;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM