我是使用 Codeigniter 作为 web api 的新手,我想得到这个结果{"result":[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}],"success":"1","message":"success"} 但我得到了这种结果:{"result":[[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}]],"success":"1","message":"success"}我想知道我哪里弄错了?我正在使用 codeigniter,下面的代码来自控制器和模型m_server.php(模式)<?php Class M_server extends CI_Model { function __construct(){ parent::__construct(); $this->load->database(); } // buat view dashboard main function dash_main1(){ $data = $this->db->query(" select * from telepon "); $result = array(); $result['result'] = array(); $result['success'] = "1"; $result['message'] = "success"; array_push($result['result'], $data->result()); return $result; } }Rest_server.php(控制器)<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Rest_server extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('m_server'); } public function index() { $this->load->helper('url'); $this->load->view('rest_server'); } function dash_main1(){ $data=$this->m_server->dash_main1(); echo json_encode($data); }}
1 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
删除此行(可选)
$result['result'] = array();
并改变这一行
$result['result'] = $data->result(); //result become the array
array_push
向现有数组添加元素
- 1 回答
- 0 关注
- 71 浏览
添加回答
举报
0/150
提交
取消