0

I am facing a problem, few days ago I had this issue which is solved but when I was retrieving data it was object so with the help of the below code I have converted that as array but now when I try to access the array I am getting Undefined index notice.

Controller

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

When var_dump in downline_income.php(view) below is the output.

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

OUTPUT

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

And on 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
.....

When try to print print_r($as['User_Objectchildren']);

A PHP Error was encountered

Severity: Notice

Message: Undefined index: User_Objectchildren

Filename: account/downline_income.php

Line Number: 43

10
  • Err, where is line 43? Commented Apr 24, 2017 at 18:27
  • print_r($as['User_Objectchildren']); is the code on #43 Commented Apr 24, 2017 at 18:28
  • What is $as? In your question there is no connection between the line with the error and the code block you provided... If you could show one code block which both defines the variable and produces the error, we could say something more useful. Commented Apr 24, 2017 at 18:39
  • @trincot : check updated question. Commented Apr 24, 2017 at 18:43
  • 1
    You did not turn it into one code block. Now what is $data? Do you understand what I am saying? Commented Apr 24, 2017 at 18:46

1 Answer 1

0

I was looking at both question and found that you can do that without creating objects. So you don't need to cast any object to array. You will get simple std array.

follow the below code.

Controller

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.