![](/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.