我是使用 Codeigniter 作為 web api 的新手,我想得到這個(gè)結(jié)果{"result":[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}],"success":"1","message":"success"} 但我得到了這種結(jié)果:{"result":[[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}]],"success":"1","message":"success"}我想知道我哪里弄錯(cuò)了?我正在使用 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 回答

飲歌長(zhǎng)嘯
TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
刪除此行(可選)
$result['result'] = array();
并改變這一行
$result['result'] = $data->result(); //result become the array
array_push
向現(xiàn)有數(shù)組添加元素
- 1 回答
- 0 關(guān)注
- 99 瀏覽
添加回答
舉報(bào)
0/150
提交
取消