Php 初始化多维数组
我试图呈现mysql查询中的值,并将它们插入到多维数组中。我能够创建数组并将查询中的所有值插入数组 当数据库中不存在值并且我尝试回显数组设置时,就会出现问题。我得到以下错误:Php 初始化多维数组,php,arrays,multidimensional-array,q,Php,Arrays,Multidimensional Array,Q,我试图呈现mysql查询中的值,并将它们插入到多维数组中。我能够创建数组并将查询中的所有值插入数组 当数据库中不存在值并且我尝试回显数组设置时,就会出现问题。我得到以下错误: Undefined offset: 2 如果我让你知道我的意思可能会更容易 我的类包含许多动态回显设置值的函数 i、 e 上述值是通过以下查询中的值的动态呈现设置的: while ($row = mysqli_fetch_array ($results, MYSQLI_ASSOC)) {
Undefined offset: 2
如果我让你知道我的意思可能会更容易
我的类包含许多动态回显设置值的函数
i、 e
上述值是通过以下查询中的值的动态呈现设置的:
while ($row = mysqli_fetch_array ($results, MYSQLI_ASSOC))
{
$this->answers[$row['period_id']][] = $row['question_one'];
$this->answers[$row['period_id']][] = $row['question_two'];
}
只要数据库中存在值,此功能就可以工作。所以,我需要一种初始化数组的方法
我试过这样做
$this->answers = $this->theResponseArray();
public function theResponseArray()
{
return $array = array (
1 => array (
0 => null,
1 => null,
),
2 => array (
0 => null,
1 => null,
),
3 => array (
0 => "",
1 => "",
),
3 => array (
0 => "",
1 => "",
)
);
}
但所发生的一切是,它将整个数组呈现为null,并阻止将新值放入数组中
更新
我已将整个代码放在小提琴上:
您可以通过isset检查数组中的值是否存在,然后对其执行某些操作,例如:
public function questionOne()
{
if (isset($this->answers[$row['period_id']]['question_one']) {
return $this->answers[$row['period_id']]['question_one'];
} else {
return 'no question'; //or '', or whatever
}
}
编辑:
getQuestionWeekXyz函数唯一不同的地方是周数,所以应该将它放在一个地方。然后,您可以使用foreach浏览周表中的问题:
public function getQuestionByWeek($weekNo) {
$result = '';
//if there are no questions for this week
if (!isset($this->answers[$weekNo]) || !is_array(this->answers[$weekNo])) {
return $result;
}
$result .= "<form action='index.php?view=$this->postDestination' method='post' >";
foreach ($this->answers[$weekNo] as $questionNumber => $questionText) {
$questionNumber++;
$result .= "<p>
Question Week {$this->numberToString($weekNo)}: <textarea name='cycleWeekQuestion{$this->numberToString($questionNumber)}' cols='$this->col' rows='$this->row' style='font-family: Tahoma; font-size: 12pt' />{$questionText}</textarea>
</P>";
}
$result .= "<input type='hidden' name='cycleWeekId' value='{$weekNo}'>
<span class='hideSubmit'><input type='submit' name='submit' size='20' style='font-family: Tahoma; width: 200px; height: 25pt; font-size: 14pt' value='Submit'></span>
</form>";
return $result;
}
public function numberToString($number)
{
switch($number) {
case 1: return 'One';
case 2: return 'Two';
case 3: return 'Three';
case 4: return 'Four';
}
return $number;
}
public function questionsWeekOne()
{
return $this->getQuestionByWeek(1);
}
public function QuestionsWeekTwo()
{
return $this->getQuestionByWeek(2);
}
public function QuestionsWeekThree()
{
return $this->getQuestionByWeek(3);
}
public function QuestionsWeekFour()
{
return $this->getQuestionByWeek(4);
}
你好,斯梅戈尔。谢谢你的回复。我希望能找到一个更干净的办法来解决这个问题。例如,一个函数可能有10个问题。我不能做,如果设置了10个问题,如果你发布这个案例的完整代码,我可以告诉你更好的方式。嗨,斯梅戈尔。我已经发布了完整的代码:嗨,保罗,我已经编辑了我的答案,你应该替换代码中的那些函数。嗨,斯梅戈尔。非常感谢你。我现在正在经历它。这是一种更为优雅的辩护方式。非常感谢你
public function getQuestionByWeek($weekNo) {
$result = '';
//if there are no questions for this week
if (!isset($this->answers[$weekNo]) || !is_array(this->answers[$weekNo])) {
return $result;
}
$result .= "<form action='index.php?view=$this->postDestination' method='post' >";
foreach ($this->answers[$weekNo] as $questionNumber => $questionText) {
$questionNumber++;
$result .= "<p>
Question Week {$this->numberToString($weekNo)}: <textarea name='cycleWeekQuestion{$this->numberToString($questionNumber)}' cols='$this->col' rows='$this->row' style='font-family: Tahoma; font-size: 12pt' />{$questionText}</textarea>
</P>";
}
$result .= "<input type='hidden' name='cycleWeekId' value='{$weekNo}'>
<span class='hideSubmit'><input type='submit' name='submit' size='20' style='font-family: Tahoma; width: 200px; height: 25pt; font-size: 14pt' value='Submit'></span>
</form>";
return $result;
}
public function numberToString($number)
{
switch($number) {
case 1: return 'One';
case 2: return 'Two';
case 3: return 'Three';
case 4: return 'Four';
}
return $number;
}
public function questionsWeekOne()
{
return $this->getQuestionByWeek(1);
}
public function QuestionsWeekTwo()
{
return $this->getQuestionByWeek(2);
}
public function QuestionsWeekThree()
{
return $this->getQuestionByWeek(3);
}
public function QuestionsWeekFour()
{
return $this->getQuestionByWeek(4);
}