[英]How to access stdClass Object array in codeigniter
I have following print_r ($rolePermissions);
我有以下
print_r ($rolePermissions);
array result and wondering how can i access only permID
dynamically. 数组结果,想知道如何动态地仅访问
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 ( ) )
I am accessing with following code: 我正在访问以下代码:
$rolePermission[0]->permID
But i am unable to get all the records because results can be varied each time and index 0 is not feasible way to do this. 但是我无法获取所有记录,因为结果每次都可以更改,而索引0并非可行的方法。 It displays some records and pop up errors in others.
它显示一些记录,并在其他记录中弹出错误。
Error messages:
Undefined offset: 0
Message: Trying to get property of non-object
any help ? 有什么帮助吗?
Either loop over the array: 遍历数组:
$ids = array();
foreach($rolePermission as $permissionObject){
$ids[] = $permissionObject->permID;
}
Or check that the key exists before accessing it: 或在访问之前检查密钥是否存在:
if(isset($rolePermission[0])) {
$id = $rolePermission[0]->permID;
}
Note: When you use the print_r()
it will output the array with stdClass Object
in CI when you are pulling out a value from the Database. 注意:当您从数据库中提取值时,使用
print_r()
,它将在CI中输出带有stdClass Object
的数组。
There is a solution to display the array without using the stdClass Object
while iterating. 有一种解决方案可以在迭代时不使用
stdClass Object
显示数组。
Example: Consider $final
consider the array and while using print_r()
it displayed the stdClass Object
. 示例:考虑
$final
考虑数组,并在使用print_r()
时显示stdClass Object
。
stdClass Object
while printing it. stdClass Object
。 Code: 码:
This code you can use it for the values to retrieve from the DB using the Controller and Model. 您可以使用此代码将其用作使用Controller和Model从数据库检索的值。
If single row of the output is retrieved from the DB
如果从数据库检索到输出的单行
<?php
foreach($final->result() as $single)
{
//You can print the variable values over here as follows (E.g) echo $single->id
}
?>
If Multiple row of the output is retrieved from the DB
如果从数据库检索到输出的多行
<?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
?>
Here is the sample that your model should look like if you are using the above methods for the retrieving of the output. 如果您使用上述方法检索输出,则模型应为以下示例。
employee_model.php 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);
}
in Model write this code. 在模型中编写此代码。 last line will get the value of expire column only and will pass to controller.
最后一行将仅获取expire列的值,并将传递给控制器。 then you can access it form view
然后您可以通过表格视图访问它
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;
}
in controller pass value to view 在控制器中传递值以查看
$data['album_expire'] = $this->albums_model->album_expire($user_id ,$album_iid);
in view access it like this 在视图中这样访问
<?php echo $album_expire; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.