dongrong5189 2012-01-23 13:51
浏览 9
已采纳

将项添加到数组中

When the interpreter reaches $pDB->AddLine(5,"Test") it stops responding! It returns the following error "Fatal error: Maximum execution time of 30 seconds exceeded in ... on line 21" Am I missing something? Should I use array_push() instead?

<?php
    class pDb{
        protected $m_pArray;
        public function __construct($arr){
            $this->m_pArray = $arr;
        }
        public function RemoveLine($index){ // Todo
        }
        public function ReplaceLine($index,$input){
            if(!$this->m_pArray)return -1;
            $temp = array();
            for($i=0;$i<count($this->m_pArray);$i++){
                ($i == $index) ? $temp[$i] = $input : $temp[$i] = $this->m_pArray[$i]; 
            }
            $this->m_pArray = $temp;
        }
        public function AddLine($index,$input){
            if(!$this->m_pArray)return -1;
            $temp = array();
            for($i=0;$i<count($this->m_pArray);$i++){
                if($i == $index) { $temp[$i] = $input;$i = $i-1; }else{ $temp[$i] = $this->m_pArray[$i]; }
            }
            $this->m_pArray = $temp;
        }
        public function Get(){ if($this->m_pArray)return $this->m_pArray; return null;}
        public function GetLine($i){ if($this->m_pArray)return $this->m_pArray[$i]; return null;}
    }

    $file = file("db.ini");
    for($i=0;$i<count($file);$i++){
        echo $i.": | ".$file[$i]."<br/>";
    }

    echo "<br/>===================================================================================================================<br/><br/>";

    $pDB = new Pdb($file);
    #$pDB->ReplaceLine(5,"Test"); // Works!!!
    $pDB->AddLine(5,"Test"); // Crash!!!
    for($i=0;$i<count($pDB->Get());$i++){
        echo $i.": | ".$pDB->GetLine($i)."<br/>";
    }
?>

Fix : Change

for($i=0;$i<count($this->m_pArray);$i++){
                if($i == $index) { $temp[$i] = $input;$i = $i-1; }else{ $temp[$i] = $this->m_pArray[$i]; }
            }

to

        $done=0;
        for($i=0;$i<count($this->m_pArray)+1;$i++){
            if($i == $index && $done!=1){ $temp[$index] = $input; $done=1;}elseif($done == 1){ $temp[$i] = $this->m_pArray[$i-1]; }else{ $temp[$i] = $this->m_pArray[$i]; }
        }
  • 写回答

2条回答 默认 最新

  • duanhuang7591 2012-01-23 13:58
    关注

    Consider your code...

    for($i=0;$i<count($this->m_pArray);$i++) {
      if($i == $index) {
        $temp[$i] = $input;
        $i = $i-1;
      } else {
        $temp[$i] = $this->m_pArray[$i];
      }
    }
    

    If $i == $index, then you promptly subtract one from $i, and go around the loop again. This adds one to $i, making it equal to $index again, and you fall into the same case - forever! You either need to tie your loop condition to something you change in the if branch (i.e. $temp), or change the logic here completely.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置