问题
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] => 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
.....
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
回答1:
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;
}
来源:https://stackoverflow.com/questions/43595202/undefined-index-issue-after-casting-from-object-to-array