从对象转换为数组后的未定义索引问题

时间:2017-04-24 18:23:16

标签: php arrays codeigniter object casting

我遇到了一个问题,前几天我遇到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_dump (查看)中的downline_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] => kapil789654@abc.com
                    [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

     

文件名:account / downline_income.php

     

行号:43

1 个答案:

答案 0 :(得分: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;
}