从数组对象中提取数据

时间:2015-10-21 20:22:46

标签: php arrays

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

这是代码..

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 - model

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_data] =&gt;数组([0] =&gt; stdClass对象([subject_id] =&gt; 2)[1] =&gt; stdClass对象([subject_id] =&gt; 3)))< / p>

发生数据库错误

错误号码:1054

未知列&#39;数组&#39;在&#39; where子句&#39;

SELECT fac_id FROM tbl_subjects WHERE id = Array

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

行号:42

2 个答案:

答案 0 :(得分:0)

看起来你正在寻找一个&#39; stud_id&#39;或者&#39; id&#39;值为数组。

你应该修改你的陈述:

<title>LOST Collector</title>
 <link rel="stylesheet" type="text/css" href="main.css"/>        
</head>
    <body>
        <!--Header-->
        <header>    
            <a href="http://www.lostcollector.com">
                <img src="images/logo.png" alt="Lost Collector" title="Lost Collector"/>
            </a>
            <!--Primary navigation-->
            <nav>
                <ul>
                    <li><a href="template.html">Home</a></li>
                    <li><a href="template.html">About Us</a></li>
                    <li><a href="template.html">Contact</a></li>
                </ul>
            </nav>
        </header>
         <!-- Secondary Navigation -->
        <ul id="navigation_layout">
          <li><a href="artwork.html">Artwork</a></li>
          <li><a href="autographs.html">Autographs</a></li>
          <li><a href="booksandmagazines.html">Books/Magazines</a></li>
          <li><a href="clothing.html">Clothing</a></li>
          <li><a href="dvdsandcds.html">Dvds and Cds</a></li>
          <li><a href="filmcrew.html">Film Crew</a></li>
          <li><a href="originalprops.html">Original Props</a></li>
          <li><a href="specialevents.html">Special Events</a></li>
          <li><a href="toysandgames.html">Toys and games</a></li>
          <li><a href="tradingcards.html">Trading cards</a></li>
          <li><a href="everythingelse.html">Everything else</a></li>
          <li><a href="wishlist.html">Wish list</a></li>
        </ul>

选择&#39; $ data&#39;

中的特定内容

答案 1 :(得分:0)

为了使用PHP代码将std对象转换为数组,您可以将对象转换为json对象,这会将对象的字符串返回到json格式。

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

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