PHP 读取 txt 文件并将数据存储在哈希表中


PHP Reading txt file and store data in hashtable

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

ProductID|Platform|TitleID|Cat|Barcode|..
TitleID|TitleArticle|TitleName|..
ProductID|ImgID|Img|ImgType|..
ProductID|AnnotationID|AnnotationType|AnnotationText|..
ProductGenreID|ProductID|Genre1|...

每个大约 22000 行。我想读取这些文件并将其数据存储在数据库中。但是正如您所看到的,这些文件在 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实际上得到了我的数据库游戏表所需的字段,在这里检查架构

创建表games ( id int(11( 不为空AUTO_INCREMENT, api int(11( 默认空, title 微小文本字符集拉丁语1, titleID int(11( 默认空, desc文本字符集拉丁语1, console_id int(11( 默认空, genre_id int(11( 默认空, publisher varchar(255( 字符集拉丁语 1 默认空, developers 瓦尔查尔(255( 默认空, barcode 瓦尔查尔(255( 不为空, image_url varchar(255( 默认空, screenshot 瓦尔查尔(999( 默认空, status int(1( 默认值 '0', release_date时间戳 空默认值 空, created时间戳不为空默认CURRENT_TIMESTAMP更新CURRENT_TIMESTAMP, modified时间戳 空默认值 空, 主键 ( id (( 引擎=InnoDB AUTO_INCREMENT=3075 默认字符集=UTF8;

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

  1. 抓取每一行并解释数据类型
  2. 使用表中的 ID 和结果类型将行存储在表中
  3. 对每个项目运行不同的查询,并立即引用数据库数据,以便您可以建立链接
  4. 解析数据并再次插入到表中

我想我的意思是将数据放入数据库,以便您可以在非常基本的级别上引用它。然后解析和关联数据。通过这种方式,您可以以非线性方式跳转数据,而不是在文本中大量循环。

  1. 循环遍历父产品文件并将其数据存储在数据库中。
  2. 循环遍历子文件并将其数据存储在数据库中。
  3. 遍历任何其他文件并将其数据存储在数据库中。
无需在插入

父记录后立即插入相关的子记录。