从对象到数组的转换后未定义的索引问题 [英] undefined index issue after casting from object to array
问题描述
几天前我遇到了一个问题,这问题已解决,但是当我检索数据时它是对象,因此借助以下代码,我已将其转换为数组,但是现在当我尝试访问数组时,我得到 Undefined index
通知。
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.
控制器
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错误
A PHP Error was encountered
严重性:通知
消息:未定义索引:User_Objectchildren
Message: Undefined index: User_Objectchildren
文件名:account / downline_income.php
Filename: account/downline_income.php
行号:43
推荐答案
我正在看两个问题并找到了无需创建对象就可以做到这一点。因此,您无需投射
要数组的任何对象。您将获得简单的 std a rray
。
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
.
遵循以下代码。
控制器
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;
}
这篇关于从对象到数组的转换后未定义的索引问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!