如何在codeigniter中访问stdClass对象数组

时间:2014-08-22 01:36:02

标签: php arrays 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

任何帮助?

3 个答案:

答案 0 :(得分:1)

循环遍历数组:

$ids = array();
foreach($rolePermission as $permissionObject){
    $ids[] = $permissionObject->permID;
}

或者在访问密钥之前检查密钥是否存在:

if(isset($rolePermission[0])) {
    $id = $rolePermission[0]->permID;
}

答案 1 :(得分:0)

注意:当您使用print_r()时,当您从数据库中提取值时,它会在CI中输出stdClass Object的数组。

有一种解决方案可以在迭代时不使用stdClass Object来显示数组。

示例:考虑$final考虑数组,在使用print_r()时,它会显示stdClass Object

  • 您必须按如下方式使用循环,以便在打印时避免使用stdClass Object

<强>代码:

您可以使用此代码将其用于使用Controller和Model从数据库中检索的值。

  

如果从DB中检索单行输出

<?php
foreach($final->result() as $single)
{
   //You can print the variable values over here as follows (E.g) echo $single->id    
}
?>
  

如果从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
?>

如果在foreach中使用` - &gt; result()`来获取值

,那么模型代码应该如何?

如果您使用上述方法检索输出,那么您的模型应该是这样的示例。

<强> 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);
 }

答案 2 :(得分:0)

在模型中

编写此代码。最后一行只会获得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; ?>