duanpi7578 2017-06-23 02:22
浏览 34
已采纳

制作对象并在codeigniter视图上传递它

i have a query from my model. Now in my controller, the query is inside the loop, now i have two results of objects from my query. How can i make it only one object to be passed in my view?

Here is my code:

public function graph_diagnose() {
       $diagnosis = $this->input->post('selectdiagnosis');
       for ($i = 0; $i < count($diagnosis); $i++) {
            $diagname = $this->Adminreport_model->getDiagName($diagnosis[$i]);
            echo json_encode($diagname);
       }
    }

now i have a result from my query that is stored in $diagname and it consist of two rows inside every query.

Here is the result of echo json_encode:

 array(1) {
  [0]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(8) "Headache"
    ["diagnosis_id"]=>
    string(1) "1"
  }
}
array(1) {
  [0]=>
  object(stdClass)#24 (2) {
    ["diagnosis"]=>
    string(10) "Sakit Kaau"
    ["diagnosis_id"]=>
    string(1) "2"
  }
}

now what i want is to make it as one object to be json encode. where i want an output like this:

 array(1) {
  [0]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(8) "Headache"
    ["diagnosis_id"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(10) "Sakit Kaau"
    ["diagnosis_id"]=>
    string(1) "2"
  }
}
  • 写回答

1条回答 默认 最新

  • dqce48404 2017-06-23 02:37
    关注

    Define an array to store the result:

    $diagnames = [];
    

    Inside the for loop:

    $result = $this->Adminreport_model->getDiagName($diagnosis[$i]);
    $diagnames = array_merge($diagnames, $result);
    

    But I am wondering why your getDiagName() method is returning an array?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: