初始化多维数组

i am trying to render values from mysql query and insert them into a multidimensional array. I am able to create the array and insert all the values from the query into the array.

the problem occurs when a value does not exist in the database and i try to echo out the array setting. I get the followig error:

Undefined offset: 2 

might be easier if i show you want i mean

my class contains lots of functions that echo dynamically set values i.e

public $answer=array();

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

the above values are set from the dynmic rendering of values from the query below:

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 fuction works as long as the values exist in the database. So, i need a way to initlize the array.

i tried doing this ;

$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 => "",
                                       )
                  ); 
    }

but all that happened is that it rendered the entire array as null and prevented from placing new values into the array.

UPDATE

i have placed the entire code on fiddle:

the code

You can check if value in array exists (by isset), and then do something with it, for example:

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
  }
} 

Edit:

The only thing that is different bout your getQuestionWeekXyz function is the number of week, so you should put it in one place. Then you can use foreach to go through questions in your week table:

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);
}