Question

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.

Was it helpful?

Solution

The statement

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

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

   for ($k = 0, $m = count($lines) - 1; $k < $m; $k++) {

So, after revision the reading loop should (I think) be like this:

$fp = fopen('db.csv','r') or die("can't open file");
$k=0;
while($lines = fgetcsv($fp)) {
            $one[$k] = $lines[0];
            $two[$k] = $lines[1];
            $three[$k] = $lines[2];
            $four[$k] = $lines[3];
            $five[$k] = $lines[4];
            $six[$k] = $lines[5];
    $k++;
    $countLines++;
}

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

/*
 * Store data in file
 */
$fp = fopen('db2.csv', 'w') or die("can't open file");
fwrite($fp, $header);

/*
 * Data for export
 */
for ($i = 0, $j = $countLines - 1; $i < $j; $i++) {
    fprintf($fp, "%s %s %-3s %-30s %-30s %-30s\r\n", /* possibly add commas here? */
            $one[$i], $two[$i], $three[$i],$four[$i], $five[$i], $six[$i]);

}

OTHER TIPS

I get the same one and look for a solution as you ... In fact it's really simple and diabolic :fgetcsv() add an array with one NULL value at the end of the reading. Therefore, $lines[1] will produce an error, because the last added array has only one value.

Just count the number of columns in $lines like this :

/*
* 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)) {

    if ( count($lines) == 6 )
    {

        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++;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top