Okay, I’ve spent several hours on this problem and I’m not sure what’s going on. I think I just need a fresh perspective on this problem especially since I’ve been up for over 24 hours and the deadline for this is in five hours.
I am getting an Undefined offset notice for every single offset (0 to 907) when I try to use the data I pulled from a CSV. (It probably means I am not successfully pulling the data, but I am exhausted and would appreciate some help)
Does anyone know what I’m doing wrong?
$lines = array();
$lines2 = "";
$one = array();
$two = array();
$three = array();
$four = array();
$five = array();
$six = array();
$header = "";
$footer = "";
$countLines = 0;
/*
* Open the file and store its data into an array
*/
$fp = fopen('db.csv','r') or die("can't open file");
while($lines = fgetcsv($fp)) {
for ($k = 0, $m = count($lines) - 1; $k < $m; $k++) {
$one[$k] = $lines[0];
$two[$k] = $lines[1];
$three[$k] = $lines[2];
$four[$k] = $lines[3];
$five[$k] = $lines[4];
$six[$k] = $lines[5];
}
$countLines++;
}
fclose($fp) or die("can't close file");
/*
* Set up file header
*/
$header = "Header"
;
/*
* Set up file footer
*/
$footer = "Footer";
/*
* Prepare data for export
*/
for ($i = 0, $j = $countLines - 1; $i < $j; $i++) {
$lines2 .= $one[$i] ." ".
$two[$i] ." ".
str_pad($three[$i], 3) ." ".
str_pad($four[$i], 30) ." ".
str_pad($five[$i], 30) ." ".
str_pad($six[$i], 30) ."\r\n";
}
/*
* Store data in file
*/
$fp = fopen('db2.csv', 'w') or die("can't open file");
fwrite($fp, $header);
fwrite($fp, $lines2);
fwrite($fp, $footer);
fclose($fp) or die("can't close file");
The CSV file is a standard comma-delimited file so I don’t see any reason to post that data here.
The statement
fgetcsv will return a single line in the form of an array containing the elements on the line;
Therefore the following is wrong and needs to be removed as you are iterating over a single line in the CSV
So, after revision the reading loop should (I think) be like this:
After this use, e.g. print_r($one) for debug to view the arrays.
To output it I’m relying heavily on guesswork as to what you want to achieve, because you are outputting to db2.csv, but without commas (to seperate). however try something like the following