PHP在哈希表中读取文本文件和存储数据

时间:2013-12-08 07:53:54

标签: php file-io hashtable

我有几种格式的.txt文件:

ProductID|Platform|TitleID|Cat|Barcode|..

TitleID|TitleArticle|TitleName|..

ProductID|ImgID|Img|ImgType|..

ProductID|AnnotationID|AnnotationType|AnnotationText|..

ProductGenreID|ProductID|Genre1|...

每个大约22000行。我想读取这些文件并将其数据存储在db中。但正如您所看到的,文件在ProductID和TitleID的基础上是相互关联的,所以当我遍历父产品文件然后将PID传递给子文件以找到相应的记录时,它将为每个文件再循环22000次。耗费时间太长,需要数天才能完成。

无论如何,我的想法是使用PHP哈希表来存储这些文件,然后搜索记录 - 我想这种方法会降低我当前脚本的复杂性(你认为这是最好的路径吗?如果不,你有什么建议?)

如果是,我不确定如何在PHP中实现这一点。

@Ahmed和@Oswald问题是我没有文件建议的相同Db架构,这里我也粘贴了一些代码以便更好地理解..

public function getGames()
{
    $resource = self::DATAFILES.'data sample\Product.txt';

    $games = array_slice($this->readFile($resource), 1); 
    $data = array();
    $count = 1;
    foreach($games as $records)
    {
        $game = new Games();
        $attributes = explode($this->delimiter,$records);
        $game->api   =  (int) $attributes[0];
        echo $game->title = (string) $this->getTitle($attributes[2]);
        $game->titleID = (string) $attributes[2];
        $game->desc = (string) $this->getDescription($attributes[0]);
        $game->console = (string) $attributes[1];
        $game->genre = (string) implode(',', $this->getProductGenre($attributes[0]));
        $game->screenshot = (string) $this->getScreenshot($attributes[0]);
        $game->publisher =  (string) $this->getCompany($this->getPublisher($attributes[0]));
        $game->developers =  (string) $this->getCompany($this->getDeveloper($attributes[0]));
        $game->barcode = (string) $attributes[4];
        $game->image = $this->getCoverImage($attributes[0]);
        $game->releaseDate = strtotime($attributes[8]);
        $data[] = $game;
        //if($count == 1000): break; else: $count++; endif;
    }
        return $data;

}
public function getTitle($titleID)
{
    $resource = self::DATAFILES.'data sample\Title.txt';

    $titles = array_slice($this->readFile($resource), 1); 

    foreach($titles as $records)
    {
        $attributes = explode($this->delimiter,$records);

        $pattern = '/^' . preg_quote($attributes[0], '/') . '$/';
        if (preg_match($pattern, $titleID))
        {
            return $attributes[2];
            break;
        }

    }

}

所以返回的$ data实际上得到了我在db games表中需要的字段,检查架构

CREATE TABLE games(   id int(11)NOT NULL AUTO_INCREMENT,   api int(11)DEFAULT NULL,   title tinytext CHARACTER SET latin1,   titleID int(11)DEFAULT NULL,   desc文字CHARACTER SET latin1,   console_id int(11)DEFAULT NULL,   genre_id int(11)DEFAULT NULL,   publisher varchar(255)CHARACTER SET latin1 DEFAULT NULL,   developers varchar(255)DEFAULT NULL,   barcode varchar(255)NOT NULL,   image_url varchar(255)DEFAULT NULL,   screenshot varchar(999)DEFAULT NULL,   status int(1)DEFAULT'0',   release_date时间戳NULL DEFAULT NULL,   created时间戳NOT NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,   modified时间戳NULL DEFAULT NULL,   主要关键(id) )ENGINE = InnoDB AUTO_INCREMENT = 3075 DEFAULT CHARSET = utf8;

2 个答案:

答案 0 :(得分:0)

也许一个过程使用了几个步骤?

  1. 抓住每一行并解释数据类型
  2. 使用表格中的ID和结果类型将行存储在表格中
  3. 为每个项目运行不同的查询并立即引用数据库数据,以便您可以建立链接
  4. 解析数据并再次插入表格
  5. 我想我得到的是将数据导入数据库,以便您可以在非常基础的级别上引用它。然后解析并关联数据。这样,您可以以非线性方式跳过数据,而不是通过文本进行大量循环。

答案 1 :(得分:0)

  1. 循环访问父产品文件并将其数据存储在数据库中。
  2. 遍历子文件并将其数据存储在数据库中。
  3. 遍历任何其他文件并将其数据存储在数据库中。
  4. 插入父记录后,无需立即插入相关的子记录。