初始化多维数组

时间:2015-04-15 13:21:54

标签: php arrays multidimensional-array q

我正在尝试从mysql查询中呈现值并将它们插入到多维数组中。我能够创建数组并将查询中的所有值插入到数组中。

当数据库中不存在值并且我尝试回显数组设置时,会出现问题。我收到了以下错误:

Undefined offset: 2 
如果我表明你想要我的意思,

可能会更容易

我的班级包含许多回复动态设置值的函数 即

public $answer=array();

public function questionOne()
{
    return  $this->answers[$row['period_id']]['question_one'];
} 

上述值是根据以下查询的值的动态渲染设置的:

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并阻止将新值放入数组中。

更新

我把整个代码放在小提琴上:

the code

1 个答案:

答案 0 :(得分:0)

您可以检查数组中的值是否存在(通过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);
}