Php 从数组对象中提取数据

Php 从数组对象中提取数据,php,arrays,Php,Arrays,如何提取或获取此数组对象中的数据?因为我得到了一个数据库错误,其中数据只是一个数组 这是密码 Welcome.php-controller public function test() { $this->load->model('Crud'); $username = $this->session->userdata('username'); $data['sc_data'] = $this->select2($username);

如何提取或获取此数组对象中的数据?因为我得到了一个数据库错误,其中数据只是一个数组

这是密码

Welcome.php-controller

public function test()
{
    $this->load->model('Crud');
    $username = $this->session->userdata('username');
    $data['sc_data'] = $this->select2($username);
    $data2['t_data'] = $this->select($data);
    $this->load->view('quiz', $data2);
}

private function select($data)
{
    $result = $this->Crud->t_select($data);
    return $result;
}

private function select2($data)
{
    $result = $this->Crud->s_course($data);
    return $result;
}
crud.php-模型

public function t_select($data=array())
{
    print_r($data);
    $this->db->select('fac_id');
    $this->db->where('id', $data);
    $query = $this->db->get('tbl_subjects');
    return $query->result();

}

public function s_course($data = array())
{
    $this->db->select('subject_id');
    $this->db->where('stud_id', $data);
    $query = $this->db->get('tbl_student_subject');
    return $query->result();
}
这就是错误

数组([sc_数据]=>Array([0]=>stdClass对象([subject_id]=>2)[1]=>stdClass对象([subject_id]=>3)))


发生数据库错误

错误号码:1054

“where子句”中的未知列“Array”

tbl\u主题
中选择
fac\u id
,其中
id
=
Array

文件名:C:/wamp/www/sample/application/models/crud.php


行号:42

看起来您正在查找一个“stud\u id”或具有数组值的“id”

您应该修改您的声明:

this->db->where('stud_id', $data);
$this->db->where('id', $data);

要从“$data”中选择特定的内容,以便使用php代码将std对象转换为数组,您可以将对象转换为json对象,这将返回一个json格式的对象字符串

获取该结果并使用一个额外的参数true进行解码,该参数将转换为关联数组


$array=json_decode(json_encode($stdObject),true)

先生,我应该把这个密码放在哪里?进入控制器?或者放入模型中?您必须将此代码放在$data attr上。在这之后,编辑这一行:在这之后,print_t函数将用php打印数组。在数组中,您可以像这样获取数据:foreach$data作为$dat{echo$dat[''subject_id]}您不能使用数组对数据库进行查询,这必须是作为字符串的数据。从tbl_subjects中选择fac_id,其中id=2