Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中获取值_Php_Codeigniter - Fatal编程技术网

Php 从数组中获取值

Php 从数组中获取值,php,codeigniter,Php,Codeigniter,我是CodeIgniter的新手。在模型中,我有以下代码: public function get_all_subjects() { return $this->db->get('subjects'); } 在控制器中,我有: public function index() { $this->load->model('subjects'); $data['content'] = $this->subjects->get_all_subjec

我是CodeIgniter的新手。在模型中,我有以下代码:

public function get_all_subjects()
{
   return $this->db->get('subjects');
}
在控制器中,我有:

public function index()
{
   $this->load->model('subjects');
   $data['content'] = $this->subjects->get_all_subjects();

   $this->load->view('home', $data);
}
我正在尝试获取视图中的值:

foreach($content as $val)
{
   echo $val['subject']; //i am getting error, Message: Undefined index: subject
}
subject
表中的字段是
subject\u id
subject

我收到以下错误消息:

未定义索引:主题

您没有返回查询的结果。您刚刚运行了查询


您没有返回查询的结果。您刚刚运行了查询。

返回$this->db->get('subjects')->result_array()
应该可以做到这一点,并且可以避免创建不必要的变量:
$q
。也许@DeiForm想要更新他的函数。是的,我听说过这种链接方法。但是对于初学者来说,一步一步的方法很容易理解
return$this->db->get('subjects')->result_array()
应该可以做到这一点,并且可以避免创建不必要的变量:
$q
。也许@DeiForm想要更新他的函数。是的,我听说过这种链接方法。但对于初学者来说,循序渐进的方法很容易理解
public function get_all_subjects()
    {
        return $this->db->get('subjects')->result_array();
    }