您现在的位置是:首页 > 电脑 > 

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

2025-07-16 15:22:06
PHP非法偏移类型错误(PHP illegal offset type error) 在第二个foreach循环中,在此行上获取非法偏移类型错误。 $userlist[$user]->addPeriod($period); 从过去的线程中给出的过去信息做了一些更改,这是代码的新版本。 还有一个警告但我认为如果错误得到解决可能会解决:
PHP非法偏移类型错误(PHP illegal offset type error)

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

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

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

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

$periods_arr = array(1, 2, , 4, 5); $subPeriods_arr = array(1, 2); $questilist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q__1", "q_4_1", "q_5_1"); class User { public $userId; public $periods = array(); public function __ctruct($number) { $this->userId = $number; } public function addPeriod($pno) { $this->periods[] = new Period($pno); } } class Period { public $periodo; public $subPeriods = array(); public function __ctruct($number) { $this->periodo = $number; } public function addSubPeriod($spno) { $this->subPeriods[] = new SubPeriod($spno); } } class SubPeriod { public $SubPeriodo; public $answers = array(); public function __ctruct($number) { $this->SubPeriodo = $number; } public function addAnswer($answer) { $this->answers[] = new Question($answer); } } class Question { public $answer; public function __ctruct($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($questilist as &$aquestion) { $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' . $user . ' AD answer_sub_period = ' . $subPeriod . ' AD answer_period = ' . $period .''; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[$user]->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$questionumber]); } } } } } $userlist[]->periods[]->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, , 4, 5); $subPeriods_arr = array(1, 2); $questilist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q__1", "q_4_1", "q_5_1"); class User { public $userId; public $periods = array(); public function __ctruct($number) { $this->userId = $number; } public function addPeriod($pno) { $this->periods[] = new Period($pno); } } class Period { public $periodo; public $subPeriods = array(); public function __ctruct($number) { $this->periodo = $number; } public function addSubPeriod($spno) { $this->subPeriods[] = new SubPeriod($spno); } } class SubPeriod { public $SubPeriodo; public $answers = array(); public function __ctruct($number) { $this->SubPeriodo = $number; } public function addAnswer($answer) { $this->answers[] = new Question($answer); } } class Question { public $answer; public function __ctruct($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($questilist as &$aquestion) { $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' . $user . ' AD answer_sub_period = ' . $subPeriod . ' AD answer_period = ' . $period .''; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[$user]->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$questionumber]); } } } } } $userlist[]->periods[]->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);

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697368.html

相关标签:无
上传时间: 2023-08-27 09:46:35
留言与评论(共有 5 条评论)
本站网友 张玉昌
30分钟前 发表
"q_5_1"); class User { public $userId; public $periods = array(); public function __ctruct($number) { $this->userId = $number; } public function addPeriod($pno) { $this->periods[] = new Period($pno); } } class Period { public $periodo; public $subPeriods = array(); public function __ctruct($number) { $this->periodo = $number; } public function addSubPeriod($spno) { $this->subPeriods[] = new SubPeriod($spno); } } class SubPeriod { public $SubPeriodo; public $answers = array(); public function __ctruct($number) { $this->SubPeriodo = $number; } public function addAnswer($answer) { $this->answers[] = new Question($answer); } } class Question { public $answer; public function __ctruct($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($questilist as &$aquestion) { $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' . $user . ' AD answer_sub_period = ' . $subPeriod . ' AD answer_period = ' . $period .''; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $userlist[$user]->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$questionumber]); } } } } } $userlist[]->periods[]->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
本站网友 心做し
25分钟前 发表
but you're fetching it as a value. Try something like this
本站网友 怎么取消焦点
0秒前 发表
"q_2_2"
本站网友 menu键
7分钟前 发表