2017-04-24 34 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

感謝它解決了我的問題。 –