我需要 PHP 帮助将 CSV 文件打开到数组中,更改数据,然后用更改后的数据覆盖原始 CSV 文件
我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您以错误的方式使用
fgetcsv()
并让它一次读取多行。修改您的代码以使用:
并在每次迭代中一次解析一行。
如果你想将其写回 CSV 文件,你应该能够执行以下操作:
You're using
fgetcsv()
in the wrong way and having it read in multiple lines at once.Modify your code to use:
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: