dpzff20644 2015-04-15 13:21
浏览 66
已采纳

初始化多维数组

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

  • 写回答

1条回答 默认 最新

  • drqyxkzbs21968684 2015-04-15 13:27
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站