【问题标题】:Explode a .txt files into multidimenional array将 .text 文件分解为多维数组
【发布时间】:2020-03-05 15:02:48
【问题描述】:

目前,我正在读取一个存放我所有 .txt 文件的文件夹,然后读取所有文件数据并将其存储到数据库中。这是我从文件中获取所有数据并将其解析到数组的代码:

/**
     *
     * @param array $paths [files/8754D394_20200219131722_oplog.txt]
     * @param string $file_name [8754D394_20200219131722]
     * @param string $file_name [8754D394_20200219131722]
     * @param array $explodes [[1 => 8754D394, 2 => 20200219131722]]
    */
    public function index()
    {
      $paths = glob('files/*.txt');
      foreach ($paths as $key =>$path) {
        // files name and storing it to array
        $file_name = basename($path, '_oplog.txt');
        $explodes = explode( '_', $file_name );
        $data_file = array();
        $data_file[$key] = $explodes;

        // Date
        $full_date = substr($data_file[$key][1], 0, -6);
        $years = substr($full_date, 0, -4) . '-';
        $months = substr($full_date, 4, -2) . '-';
        $days = substr($full_date, 6);
        $full_date = $years . $months . $days;

        // storing data to database
        $fileDatabase = new File();
        $fileDatabase->name = $file_name;
        $fileDatabase->vei_id = $data_file[$key][0];
        $fileDatabase->file_date = $full_date;
        // $fileDatabase->save();


          $content = file_get_contents($path);
          $content = str_replace(array("\n", "\r"), '', $content);
          $content = explode(';', $content);
          unset($content[count($content) - 1]);

          $data_content = array();
          $data_content[$key] = $content;
          foreach ($data_content[$key] as $row) {
            if($row == '\n') {
                $data_content[] = $row;
            }
          }
        echo "<pre>";
        print_r($data_content);
        echo "</pre>";
      }
    }

问题是我需要,我的数组看起来像这样:

file1.txt

Array
(
    [0] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )
    [1] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )
)

file2.txt

Array
(
    [0] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )
)

file3.txt

Array
(
    [0] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )
    [1] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )
    [2] => Array
        (
            [0] => 19/02/2020 12:17:05
            [1] => OBD
            [2] => SetSpeedLimit
            [3] => FR
            [4] => HW3
            [5] => 
        )

)

等等……

但我就是这样:

[1] => Array
        (
            [0] => 25/01/2020 08:57:53
            [1] => OBD
            [2] => FLASHWrite
            [3] => MR
            [4] => A7
            [5] => 
            [6] => 25/01/2020 09:08:58
            [7] => OBD
            [8] => FLASHWrite
            [9] => MR
            [10] => A7
            [11] => 
            [12] => 25/01/2020 09:16:39
            [13] => OBD
            [14] => FLASHWrite
            [15] => MR
            [16] => A7
            [17] => 
            [18] => 25/01/2020 10:48:45
            [19] => OBD
            [20] => FLASHWrite
            [21] => MR
            [22] => A7
            [23] => 
        )

)

如果能提供任何帮助,我将不胜感激。

【问题讨论】:

  • $path 来自哪里,它不是对象属性,也没有作为参数传递给index 方法?
  • 它来自我名为“files”的公共文件夹
  • 现在可以了,在你完全改变了你展示给我们的代码之后

标签: php arrays laravel file


【解决方案1】:

主要问题是使用file_get_contents读取一个csv文件,根本不合适。此外,如果在分解字符串之前删除换行符,则获得多维数组的机会很小。 相反,使用fgetcsv; 作为分隔符逐行读取文件。

public function index()
{
    foreach (glob('files/*.txt') as $index => $path) {
        $filename = basename($path, '_oplog.txt');
        list($id, $date) = explode('_', $filename);
        $content = array();

        $date = DateTime::createFromFormat('Ymd+', $date)->format('Y-m-d');

        // storing data to database
        $fileDB = new File();
        $fileDB->name = $filename;
        $fileDB->vei_id = $id;
        $fileDB->file_date = $date;
        // $fileDB->save();

        // your files are csv files, read them line by line with fgetcsv
        if ( false !== $handle = fopen($path, 'rb') ) {
            while ( false !== $fields = fgetcsv($handle, 0, ';') ) {
                array_pop($fields);
                $content[] = $fields;
            }
            fclose($handle);

            echo '<pre>', print_r($content, true), '</pre>';
        } // else you can throw an exception if you can't read the file
    }
}

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2011-11-26
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 2019-06-13
  • 2011-08-17
  • 1970-01-01
相关资源
最近更新 更多