我不知道为什么当我回显 json_encode 查询结果集时,我会在每个对象之前获得结果行的编号。我只想计算返回的总行数,并让它们在 JSON 字符串的开头只显示一次,然后只返回行。即使用以下代码:
//...active record query
$result = $this->db->get();
$data = array();
$count = 1;
foreach($result->result() as $row)
{
$data['count'] = $count;
$entry = array();
$entry['firstname'] = $row->first_name;
$entry['lastname'] = $row->last_name;
$entry['jobtitle'] = $row->title;
$entry['dept'] = $row->dept_name;
$entry['deptid'] = $row->dept_no;
if($row->emp_no == null)
{
$entry['ismanager'] = 0;
}
else
{
$entry['ismanager'] = 1;
}
$data[] = $entry;
$count++;
}
return $data;
然后在控制器中对其进行json_encode,我得到:
{"count":35,"0":{"firstname":"Georgi","lastname":"Facello","jobtitle":"Senior Engineer","dept":"Development","deptid":"d005","ismanager":0},"1":{"firstname":"Kirk","lastname":"Facello","jobtitle":"Senior Engineer","dept":"Development","deptid":"d005","ismanager":0},....rest of the query results
我不想要的是"0"
等"1"
,在行结果之前。我已经有了返回结果的总数,所以我不需要单独的行号。
如果有人可以帮助我,我将不胜感激,谢谢。