我需要 PHP 帮助将 CSV 文件打开到数组中,更改数据,然后用更改后的数据覆盖原始 CSV 文件

发布于 2024-11-16 06:19:32 字数 1786 浏览 2 评论 0原文

我需要从 CSV 文件中提取数据以用于报告。但是,导出软件无法以有用的格式导出它。使用 PHP,我想获取一个包含如下数据的 CSV 文件:

'Time','Month And Year','Count'
'8 Hours','Apr 2011','127'
'6 Hours','Apr 2011','45'
'4 Hours','Apr 2011','23'
'2 Hours','Apr 2011','122'
'8 Hours','Dec 2010','29'
'6 Hours','Dec 2010','8'
'4 Hours','Dec 2010','1'
'2 Hours','Dec 2010','64'
etc...

并将其变成这样:

Time,Dec 2010,Jan 2011,Feb 2011,Mar 2011,Apr 2011,May 2011
8 Hours,126,47,115,77,127,110
6 Hours,45,18,62,38,42,31
4 Hours,23,3,11,8,15,18
2 Hours,122,93,185,144,128,70

目前,我有使用 fgetcsv() 打开 CSV 文件并删除不需要的字符的代码:

$lines = array();
$tempName = array();
$tempCount = array();
$tempTitle = array();
$items = array();

$fp = fopen('../data/data2.csv','r') or die("can't open data2.csv");

while($lines = fgetcsv($fp,4096,"\\n")) {

  for ($i = 0, $j = count($lines); $i < $j; $i++) {

    $lines[$i] = str_replace('\'', '', $lines[$i]);
    $lines[$i] = str_replace('A: ', '', $lines[$i]);
    $lines[$i] = str_replace('B: ', '', $lines[$i]);
    $lines[$i] = str_replace('C: ', '', $lines[$i]);
    $lines[$i] = str_replace('D: ', '', $lines[$i]);

  }
}

fclose($fp) or die("can't close volume2.csv");

这存储了 CSV 文件的行到逗号分隔的行中,这使得 $lines 数组看起来像:

Time,Month And Year,Count
8 Hours,Apr 2011,127
6 Hours,Apr 2011,45
4 Hours,Apr 2011,23
2 Hours,Apr 2011,122
8 Hours,Dec 2010,29
6 Hours,Dec 2010,8
4 Hours,Dec 2010,1
2 Hours,Dec 2010,64
etc...

现在我需要做的是以正确的顺序将数据存储在一个或多个数组中,以便能够将其导出回 CSV 文件。

我尝试使用:

for ($h = 0, $k = count($lines); $h < $k; $h++) {
  $items = preg_split("/[,]+/",$lines[$h]);
}

,然后使用 case 语句将正确的数据放入数组中,但它挂起,所以我一定做错了什么。 (更不用说这段代码没有优化。)关于

我可以从这里去哪里有什么想法吗?谢谢!

I need to pull data from a CSV file to use in reporting. However, the exporting software does not have the capability to export it in a useful format. Using PHP I would like to take a CSV file with data like:

'Time','Month And Year','Count'
'8 Hours','Apr 2011','127'
'6 Hours','Apr 2011','45'
'4 Hours','Apr 2011','23'
'2 Hours','Apr 2011','122'
'8 Hours','Dec 2010','29'
'6 Hours','Dec 2010','8'
'4 Hours','Dec 2010','1'
'2 Hours','Dec 2010','64'
etc...

And turn it into this:

Time,Dec 2010,Jan 2011,Feb 2011,Mar 2011,Apr 2011,May 2011
8 Hours,126,47,115,77,127,110
6 Hours,45,18,62,38,42,31
4 Hours,23,3,11,8,15,18
2 Hours,122,93,185,144,128,70

At the moment, I have the code to open the CSV file using fgetcsv() and remove unwanted characters:

$lines = array();
$tempName = array();
$tempCount = array();
$tempTitle = array();
$items = array();

$fp = fopen('../data/data2.csv','r') or die("can't open data2.csv");

while($lines = fgetcsv($fp,4096,"\\n")) {

  for ($i = 0, $j = count($lines); $i < $j; $i++) {

    $lines[$i] = str_replace('\'', '', $lines[$i]);
    $lines[$i] = str_replace('A: ', '', $lines[$i]);
    $lines[$i] = str_replace('B: ', '', $lines[$i]);
    $lines[$i] = str_replace('C: ', '', $lines[$i]);
    $lines[$i] = str_replace('D: ', '', $lines[$i]);

  }
}

fclose($fp) or die("can't close volume2.csv");

This stores the lines of the CSV file into comma delimited rows, which makes the $lines array look like:

Time,Month And Year,Count
8 Hours,Apr 2011,127
6 Hours,Apr 2011,45
4 Hours,Apr 2011,23
2 Hours,Apr 2011,122
8 Hours,Dec 2010,29
6 Hours,Dec 2010,8
4 Hours,Dec 2010,1
2 Hours,Dec 2010,64
etc...

Now what I need to do is store the data in the correct order inside of one or more arrays in order to be able to export it back into the CSV file.

I tried using:

for ($h = 0, $k = count($lines); $h < $k; $h++) {
  $items = preg_split("/[,]+/",$lines[$h]);
}

and then placing the correct data into arrays using a case statement, but it hangs up, so I must be doing something wrong. (Not to mention this code is not optimized.)

Any ideas on where I can go from here? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2024-11-23 06:19:32

您以错误的方式使用 fgetcsv() 并让它一次读取多行。

修改您的代码以使用:

while($line = fgetcsv($fp)) {

}

并在每次迭代中一次解析一行。

如果你想将其写回 CSV 文件,你应该能够执行以下操作:

$out = array();
while($line = fgetcsv($fp)) {
    // modify fields as you need to
    $out[] = implode(",", $line);
}

// now you can write implode("\n", $out) out as the modified data

You're using fgetcsv() in the wrong way and having it read in multiple lines at once.

Modify your code to use:

while($line = fgetcsv($fp)) {

}

and in each iteration parse a row at a time.

if you want to write this back to a CSV file you shoudl be able to do something like:

$out = array();
while($line = fgetcsv($fp)) {
    // modify fields as you need to
    $out[] = implode(",", $line);
}

// now you can write implode("\n", $out) out as the modified data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文