在第二个foreach循环中,在此行上获取非法偏移类型错误。
$userlist[$user]->addPeriod($period);从过去的线程中给出的过去信息做了一些更改,这是代码的新版本。 还有一个警告但我认为如果错误得到解决可能会解决:
$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();在非对象上调用成员函数addPeriod()
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:
$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();Call to a member function addPeriod() on a non-object
最满意答案
您使用$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);更多推荐
发布评论