从 PHP 中的目录和文本文档导入数据
Import data from directory and text documents in PHP
我想将数据从列标题名称为 ATMP 的目录中的文本文档推送到数组。在每个文档中 ATMP 位置都可以更改。
这是文本文档的样子
WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS TIDE
degT m/s m/s m sec sec degT hPa degC degC degC mi ft
116 2.8 3.0 99.00 99.00 99.00 999 9999.0 23.8 999.0 16.2 99.0 99.00
117 2.8 3.0 99.00 99.00 99.00 999 9999.0 23.8 999.0 16.2 99.0 99.00
这是我试图实现的代码
我将在 php 终端中点击 "index.php ATMP" 命令使其成为 运行
代码如下:
$dir = './files';
$files1 = preg_grep('/^([^.])/', scandir($dir));
for($x = 0; $x <= count($files1); $x++){
$f1 = fopen($dir . '/' .$files1[$x], 'r'); //
fseek($f1, 0, SEEK_SET);
while(($line = fgets($f1)) !== false){
array_push($data_array_list, $line);
}
}
这里有 2 件事我必须做
1.在将数据推送到数组之前,我必须忽略前两行
2. 我必须获得以下 ATMP
值
我不知道如何解决它。你能帮我看看怎么做吗?
我已经编写了脚本来获取您想要的数据。 (当 ATMP 列更改那里的位置时,此脚本将起作用)
删除多余的代码(用于测试目的。您也可以优化此代码)
<?php
$myFile = "t1.txt";
$lines = file($myFile);//file in to an array
//var_dump($lines);
echo "<pre>";
$top_row=$lines[0];
$top_row = preg_replace('/\s+/', ' ',$top_row);//remove duplicate spaces in lines
$position_data = explode(' ', $top_row);
foreach ($position_data as $key => $value) {
if($value=='ATMP'){
$index=$key;//here is your index or column number where ATMP exist
}
}
print_r($position_data);
unset($lines[0]);
unset($lines[1]); // we do not need these lines.
// exit;
foreach($lines as $key => $line)
{
$line = preg_replace('/\s+/', ' ',$line);//remove only duplicate or multiple spaces
$var = explode(' ', $line);
foreach ($var as $key => $value2) {
if($index == $key){
$arr[]=$value2;
}
}
// print_r($key);
// echo "<br>";
}
echo "<pre>";
print_r($arr);
我想将数据从列标题名称为 ATMP 的目录中的文本文档推送到数组。在每个文档中 ATMP 位置都可以更改。
这是文本文档的样子
WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS TIDE
degT m/s m/s m sec sec degT hPa degC degC degC mi ft
116 2.8 3.0 99.00 99.00 99.00 999 9999.0 23.8 999.0 16.2 99.0 99.00
117 2.8 3.0 99.00 99.00 99.00 999 9999.0 23.8 999.0 16.2 99.0 99.00
这是我试图实现的代码 我将在 php 终端中点击 "index.php ATMP" 命令使其成为 运行
代码如下:
$dir = './files';
$files1 = preg_grep('/^([^.])/', scandir($dir));
for($x = 0; $x <= count($files1); $x++){
$f1 = fopen($dir . '/' .$files1[$x], 'r'); //
fseek($f1, 0, SEEK_SET);
while(($line = fgets($f1)) !== false){
array_push($data_array_list, $line);
}
}
这里有 2 件事我必须做 1.在将数据推送到数组之前,我必须忽略前两行 2. 我必须获得以下 ATMP
值我不知道如何解决它。你能帮我看看怎么做吗?
我已经编写了脚本来获取您想要的数据。 (当 ATMP 列更改那里的位置时,此脚本将起作用)
删除多余的代码(用于测试目的。您也可以优化此代码)
<?php
$myFile = "t1.txt";
$lines = file($myFile);//file in to an array
//var_dump($lines);
echo "<pre>";
$top_row=$lines[0];
$top_row = preg_replace('/\s+/', ' ',$top_row);//remove duplicate spaces in lines
$position_data = explode(' ', $top_row);
foreach ($position_data as $key => $value) {
if($value=='ATMP'){
$index=$key;//here is your index or column number where ATMP exist
}
}
print_r($position_data);
unset($lines[0]);
unset($lines[1]); // we do not need these lines.
// exit;
foreach($lines as $key => $line)
{
$line = preg_replace('/\s+/', ' ',$line);//remove only duplicate or multiple spaces
$var = explode(' ', $line);
foreach ($var as $key => $value2) {
if($index == $key){
$arr[]=$value2;
}
}
// print_r($key);
// echo "<br>";
}
echo "<pre>";
print_r($arr);