我有一个代码点火器模型函数,需要像这样返回数组......
{"speakers":[{
"id":"1",
"name":"Jack",
"events":{
"id":"1"
"name":"event1",
},
{
"id":"2"
"name":"event2",
},
"status":"Active",
},
{
"id":"2",
"name":"Tom",
"events":{
"id":"3"
"name":"event3",
},
"status":"Active",
},
{
"id":"3",
"name":"Maria",
"events":{
"id":"1"
"name":"event1",
},
"status":"Active",
}]
}
这是我的代码:
function speakers()
{
$speakers=$this->db->get("speakers_tbl");
$data = array();
if($speakers->num_rows()>0)
{
foreach ($speakers->result_array() as $row)
{
$this->db->from('events_tbl');
$this->db->where('speaker_id', $row['id']);
$final = $this->db->get()->result_array();
$data[] = array_merge($row, $final);
}
return $data;
}
}
所需的输出是数据的 JSON 表示形式。
改变:
return $data;
为
return json_encode($data);
现在,如果您还想要缩进显示,例如您的示例,则在输出到浏览器时,您可以使用print "<pre>".print_r($data,true)."</pre>";