PHP非法偏移类型错误(PHP illegal offset type error)

编程入门 行业动态 更新时间:2024-10-26 06:37:40
PHP非法偏移类型错误(PHP illegal offset type error)

在第二个foreach循环中,在此行上获取非法偏移类型错误。

$userlist[$user]->addPeriod($period);

从过去的线程中给出的过去信息做了一些更改,这是代码的新版本。 还有一个警告但我认为如果错误得到解决可能会解决:

在非对象上调用成员函数addPeriod()

$periods_arr = array(1, 2, 3, 4, 5); $subPeriods_arr = array(1, 2); $questionslist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q_3_1", "q_4_1", "q_5_1"); class User { public $userId; public $periods = array(); public function __construct($number) { $this->userId = $number; } public function addPeriod($pno) { $this->periods[] = new Period($pno); } } class Period { public $periodNo; public $subPeriods = array(); public function __construct($number) { $this->periodNo = $number; } public function addSubPeriod($spno) { $this->subPeriods[] = new SubPeriod($spno); } } class SubPeriod { public $SubPeriodNo; public $answers = array(); public function __construct($number) { $this->SubPeriodNo = $number; } public function addAnswer($answer) { $this->answers[] = new Question($answer); } } class Question { public $answer; public function __construct($ans) { $this->answer = $ans; } public function getAnswer() { echo $answer; } } $userlist = array(); $sql = 'SELECT user_ref FROM _survey_1_as GROUP BY user_ref ORDER BY user_ref ASC'; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[] = new User($row['user_ref']); } foreach ($userlist as &$user) { foreach ($periods_arr as &$period) { $userlist[$user]->addPeriod($period); foreach ($subPeriods_arr as &$subPeriod) { $userlist[$user]->periods[$period]->addSubPeriod($subPeriod); foreach($questionslist as &$aquestion) { $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' . $user . ' AND answer_sub_period = ' . $subPeriod . ' AND answer_period = ' . $period .''; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[$user]->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$questionNumber]); } } } } } $userlist[3]->periods[3]->subPeriods[1]->getAnswer();

Getting an illegal offset type error on this line in the second foreach loop.

$userlist[$user]->addPeriod($period);

Made some changes from past info given in past threads and this is the new version of the code. There is also a warning but I think that might be resolved if the error is resolved:

Call to a member function addPeriod() on a non-object

$periods_arr = array(1, 2, 3, 4, 5); $subPeriods_arr = array(1, 2); $questionslist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q_3_1", "q_4_1", "q_5_1"); class User { public $userId; public $periods = array(); public function __construct($number) { $this->userId = $number; } public function addPeriod($pno) { $this->periods[] = new Period($pno); } } class Period { public $periodNo; public $subPeriods = array(); public function __construct($number) { $this->periodNo = $number; } public function addSubPeriod($spno) { $this->subPeriods[] = new SubPeriod($spno); } } class SubPeriod { public $SubPeriodNo; public $answers = array(); public function __construct($number) { $this->SubPeriodNo = $number; } public function addAnswer($answer) { $this->answers[] = new Question($answer); } } class Question { public $answer; public function __construct($ans) { $this->answer = $ans; } public function getAnswer() { echo $answer; } } $userlist = array(); $sql = 'SELECT user_ref FROM _survey_1_as GROUP BY user_ref ORDER BY user_ref ASC'; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[] = new User($row['user_ref']); } foreach ($userlist as &$user) { foreach ($periods_arr as &$period) { $userlist[$user]->addPeriod($period); foreach ($subPeriods_arr as &$subPeriod) { $userlist[$user]->periods[$period]->addSubPeriod($subPeriod); foreach($questionslist as &$aquestion) { $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' . $user . ' AND answer_sub_period = ' . $subPeriod . ' AND answer_period = ' . $period .''; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[$user]->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$questionNumber]); } } } } } $userlist[3]->periods[3]->subPeriods[1]->getAnswer();

最满意答案

您使用$user作为$userlist数组的键,但是您将其作为值获取。 尝试这样的事情:

foreach ($userlist as $user => $userVal) { ... $userlist[$user]->addPeriod($period); }

这使$user成为$userlist数组的关键。

做这样的事情会更清楚:

foreach ($userlist as $user) { }

然后不要将$user用作数组中的键,而只需使用$user作为值。 例如:

$user->addPeriod($period); ... $user->periods[$period]->addSubPeriod($subPeriod);

You're using $user as a key into your $userlist array, but you're fetching it as a value. Try something like this:

foreach ($userlist as $user => $userVal) { ... $userlist[$user]->addPeriod($period); }

This makes $user the key into your $userlist array.

It would be even clearer to do something like this:

foreach ($userlist as $user) { }

And then don't use $user as a key into your array, but just use $user as the value. For example:

$user->addPeriod($period); ... $user->periods[$period]->addSubPeriod($subPeriod);

更多推荐

本文发布于:2023-08-06 16:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   类型   PHP   illegal   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!