2017-04-24 45 views
0

我面临一个问题,前几天我已经解决了this问题,但当我检索数据时它是对象,所以在下面的代码的帮助下,我已经将其转换为数组,但现在当我尝试访问数组时,我得到了Undefined index通知。未定义的索引问题后,从对象铸造

控制器

public function downline_income($userId = null, $offset = 0) { 
     $userId = user::id(); 
     $limit = AZ::setting('record_per_page'); 
     $objUser = new User_Object; 
     $objUser->id = $userId; 
     $downline = $this->user->getDownline($objUser); 
     $downline = $this->object_to_array($downline); 
     AZ::layout('left-content', array(
      'block' => 'account/downline_income', 
      'user' => $userId, 
      'q' => $userId, 
      'data' => $downline, 
     )); 

public function object_to_array($obj) { 
    if (is_object($obj)) 
     $obj = (array) $obj; 
    if (is_array($obj)) { 
     $new = array(); 
     foreach ($obj as $key => $val) { 
      $new[$key] = $this->object_to_array($val); 
     } 
    } else 
     $new = $obj; 
    return $new; 
} 

当下面var_dumpdownline_income.php(视图)是输出。

//code 
$as = $data; 
echo "<pre>"; 
print_r($as['User_Objectchildren']); 

输出

array(3) { 
    ["User_Objectchildren"]=> 
    array(10) { 
    [0]=> 
    array(22) { 
     ["User_Objectchildren"]=> 
     array(0) { 
     } 
     ["level"]=> 
     int(1) 
     ["id"]=> 
     string(4) "1147" 
     ["gid"]=> 
     string(1) "4" 
     // 
     ... 

而且在print_r

Array 
(
    [User_Objectchildren] => Array 
     (
      [0] => Array 
       (
        [User_Objectchildren] => Array 
         (
         ) 

        [level] => 1 
        [id] => 1147 
        [gid] => 4 
        [parent_id] => 1112 
        [username] => test 9 
        [email] => [email protected] 
        [name] => test9 
        [status] => 0 
        [registerd] => 2017-04-20 09:03:10 
        [last_login] => 0000-00-00 00:00:00 
        [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7 
        [tranjection_password] => 
        [package_id] => 6 
        [user_id] => 1147 
        [purchase_date] => 2017-04-20 09:03:11 
        [confirm_date] => 0000-00-00 00:00:00 
        [package_name] => USD 1000 
        [amount] => 1000 
        [daily_income] => 12 
        [total_income] => 600 
        [time_duration] => 60 
       ) 

      [1] => Array 
       (
        [User_Objectchildren] => Array 
         (
         ) 

        [level] => 1 
        [id] => 1146 
        [gid] => 4 
        [parent_id] => 1112 
        [username] => test8 
..... 

当尝试打印print_r($as['User_Objectchildren']);

遇到

一个PHP错误

严重性:注意

消息:未定义指数:User_Objectchildren

文件名:帐号/ downline_income.php

行号:43

+0

ERR,其中是43行? – frz3993

+0

'print_r($ as ['User_Objectchildren']);'#43上的代码 –

+0

'$ as'是什么?在你的问题中,错误行与你提供的代码块之间没有任何联系......如果你可以显示*代码块,它们都定义了变量*并且*产生了错误,我们可以说更有用的东西。 – trincot

回答

0

我看着这两个问题,发现你可以做到这一点,而无需创建对象。所以你不需要cast任何对象来数组。你会得到简单的std array

请按照下面的代码。

控制器

public function downline_income($userId = null, $offset = 0) { 
    $userId = user::id(); 
    $limit = AZ::setting('record_per_page'); 
    $objUser = new stdClass(); 
    $objUser->id = $userId; 
    $downline = $this->user->getDownline($objUser); 

    AZ::layout('left-content', array(
     'block' => 'account/downline_income', 
     'user' => $userId, 
     'total_users' => $total_users, 
     'pagination' => $pagination, 
     'q' => $userId, 
     'data' => $downline, 
     'offset' => $offset, 
    )); 
} 

public function getDownline($obj, $level = 0) { 
    $obj->level = $level; 

    $where = array('parent_id' => $obj->id); 
    $this->db->select('users.*'); 
    $this->db->where($where); 

    $query = $this->db->get('users')->result(); 

    foreach ($query as $objUser) { 
     $obj->data[] = $this->getDownline($objUser, ($level + 1)); 
    } 

    return $obj; 
} 
+0

感谢它解决了我的问题。 –