dsqe46004 2015-11-23 13:49
浏览 33
已采纳

如何将非常大的文本文件存储到数组中?

I have a very large text file with more than 10.000 entries. I need to store the data into an array, because I need to check and validate all the entries before storing everything in the mySQL database.

$file_path = $filename;
$linesArray = file($file_path);    
$properties = array();

foreach ($linesArray AS $line) {
    if (strlen($line) && $line[0] == '#') {
        $pdate = substr($line, 1);
        $date = rtrim($pdate);
        $formatted = DateTime::createFromFormat('* M d H:i:s T Y',$date);
    }
    if (false !== ($pos = strpos($line, '='))) {
        $prop=array();    
        $prop[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
        $lineContArray = explode("=", $line);
        $identArray = explode(".", $lineContArray[0]);
        $ident = $identArray[0];
        $type = $identArray[1];     
        $value = trim($lineContArray[1]);
        $found = 0;
        for ($i=0; $i<count($properties); $i++) {
            if ($properties[$i]['number'] == $ident) {
                $properties[$i][$type]= $value;
                $found=1;
                break;
            }
        }
        if ($found == 0) { 
            if (!empty($type)) { 
                $properties[] = array('number' => $ident, $type => $value); 
            } else { 
                $properties[] = array($ident => $value); 
            } 
        }
    }
}
var_dump($properties);

This works very well with files up to 3000 entries, but with larger files I get a blanc page.

  • 写回答

2条回答 默认 最新

  • duangou1953 2015-11-23 14:07
    关注

    Instead of dumping the array after the whole file has been processed, you could dump in each iteration the element you are adding to that array. That way there is a flow of output:

            ...
            if ($found == 0) { 
                if (!empty($type)) { 
                    $property = array('number' => $ident, $type => $value); 
                } else { 
                    $property = array($ident => $value); 
                }
                // dump this line's result and a linebreak:
                var_dump($property);
                echo '<br>';
                $properties[] = $property;
            }
        }
    }
    // not needed anymore:
    //var_dump($properties);
    //
    

    If the output still does not come through, add flush() in your code.

    Some comments on your code

    The last of the following lines in your code will produce a run-time error when processing a line that has an equal sign, but no preceding dot:

        $identArray = explode(".", $lineContArray[0]);
        $ident = $identArray[0];
        $type = $identArray[1];
    

    The inner loop (for ($i=0; $i<count($properties); $i++)) is very inefficient. It would be better to make an associative array keyed by $ident, containing references to your $properties array. That way your look-up does not need a loop.

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

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启