![](/img/trans.png)
[英]how to access stdclass object inside stdclass object in codeigniter
[英]How to access stdClass Object array in codeigniter
我有以下print_r ($rolePermissions);
数组结果,想知道如何动态地仅访问permID
。
Array ( [0] => Array ( [0] => stdClass Object ( [roleID] => 1 [permID] => 1 ) [1] => stdClass Object ( [roleID] => 1 [permID] => 2 ) [2] => stdClass Object ( [roleID] => 1 [permID] => 3 ) [3] => stdClass Object ( [roleID] => 1 [permID] => 4 ) [4] => stdClass Object ( [roleID] => 1 [permID] => 5 ) [5] => stdClass Object ( [roleID] => 1 [permID] => 6 ) [6] => stdClass Object ( [roleID] => 1 [permID] => 7 ) [7] => stdClass Object ( [roleID] => 1 [permID] => 8 ) [8] => stdClass Object ( [roleID] => 1 [permID] => 9 ) [9] => stdClass Object ( [roleID] => 1 [permID] => 10 ) [10] => stdClass Object ( [roleID] => 1 [permID] => 11 ) [11] => stdClass Object ( [roleID] => 1 [permID] => 12 ) [12] => stdClass Object ( [roleID] => 1 [permID] => 13 ) [13] => stdClass Object ( [roleID] => 1 [permID] => 14 ) [14] => stdClass Object ( [roleID] => 1 [permID] => 15 ) [15] => stdClass Object ( [roleID] => 1 [permID] => 16 ) [16] => stdClass Object ( [roleID] => 1 [permID] => 17 ) [17] => stdClass Object ( [roleID] => 1 [permID] => 18 ) [18] => stdClass Object ( [roleID] => 1 [permID] => 19 ) [19] => stdClass Object ( [roleID] => 1 [permID] => 20 ) [20] => stdClass Object ( [roleID] => 1 [permID] => 21 ) [21] => stdClass Object ( [roleID] => 1 [permID] => 22 ) [22] => stdClass Object ( [roleID] => 1 [permID] => 23 ) [23] => stdClass Object ( [roleID] => 1 [permID] => 24 ) ) [1] => Array ( [0] => stdClass Object ( [roleID] => 2 [permID] => 2 ) [1] => stdClass Object ( [roleID] => 2 [permID] => 3 ) [2] => stdClass Object ( [roleID] => 2 [permID] => 4 ) ) [2] => Array ( [0] => stdClass Object ( [roleID] => 3 [permID] => 4 ) ) [3] => Array ( ) )
我正在访问以下代码:
$rolePermission[0]->permID
但是我无法获取所有记录,因为结果每次都可以更改,而索引0并非可行的方法。 它显示一些记录,并在其他记录中弹出错误。
Error messages:
Undefined offset: 0
Message: Trying to get property of non-object
有什么帮助吗?
遍历数组:
$ids = array();
foreach($rolePermission as $permissionObject){
$ids[] = $permissionObject->permID;
}
或在访问之前检查密钥是否存在:
if(isset($rolePermission[0])) {
$id = $rolePermission[0]->permID;
}
注意:当您从数据库中提取值时,使用print_r()
,它将在CI中输出带有stdClass Object
的数组。
有一种解决方案可以在迭代时不使用stdClass Object
显示数组。
示例:考虑$final
考虑数组,并在使用print_r()
时显示stdClass Object
。
stdClass Object
。 码:
您可以使用此代码将其用作使用Controller和Model从数据库检索的值。
如果从数据库检索到输出的单行
<?php
foreach($final->result() as $single)
{
//You can print the variable values over here as follows (E.g) echo $single->id
}
?>
如果从数据库检索到输出的多行
<?php
$row=array();
foreach($final->result() as $single)
{
//You can store it as an array here if you are going on with multiple loops
$row[] = $single;
}
print_r($row); // here you can save it as an another array
?>
如果您使用上述方法检索输出,则模型应为以下示例。
employee_model.php
<?php
class Employee_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getEmployees()
{
$this->db->select('*');
$this->db->from('employee');
$this->db->where('delete_status','0');
$this->db->where('status','1');
$this->db->order_by('id','DESC');
$query = $this->db->get();
return $query;
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Layout_Controller {
public function __construct(){
parent::__construct();
$this->load->library("pagination");
$this->load->model('employee_model');
$this->load->model('ajax_model');
}
public function employee_listing()
{
$result['all_employee'] = $this->employee_model->getEmployees(); // getEmployees is the function name in the employee_model
$this->load->view('frontend/employee_list',$result);
}
在模型中编写此代码。 最后一行将仅获取expire列的值,并将传递给控制器。 然后您可以通过表格视图访问它
function album_expire($user_id ,$album_iid)
{
$this->db->select('expire');
$this->db->from('mw_album');
$this->db->where('user_id', $user_id);
$this->db->where('id', $album_iid);
return $this->db->get()->row()->expire;
}
在控制器中传递值以查看
$data['album_expire'] = $this->albums_model->album_expire($user_id ,$album_iid);
在视图中这样访问
<?php echo $album_expire; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.