我正在从控制器JSON
中的 MySQL 数据库中以格式输出数据,如下所示,
function byId(){
$this -> load -> model('usermodel');
$data['user'] = $this -> usermodel -> getById($this->uri->slash_segment(3));
$this -> output -> set_content_type('application/json');
$this -> output -> set_output(json_encode($data));
}
模型:
function getById($id){
$this->db->select('*');
$this->db->from('members');
$this->db->where('id', $id);
$q = $this -> db -> get();
if ($q -> num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
它输出这样的数据,
{"user":[{"id":"3","firstname":"Frances","lastname":"Johnson","address":"489 Summit Lane","state":"Minnesota","country":"US","email":"fjohnson@teklist.net","phone":null,"experience":"2","designation":"Script"}]}
但我需要这样
{user : {....} }
基本上我想摆脱方括号。
我可以在我的代码中进行哪些更改以获得预期的输出?