PHP 使用 fgetcsv 未定义偏移量

发布于 2024-12-01 10:05:38 字数 1571 浏览 1 评论 0原文

好吧,我在这个问题上花了几个小时,但我不确定发生了什么。我想我只是需要对这个问题有一个新的视角,特别是因为我已经熬夜超过 24 小时,而这个问题的最后期限是 5 小时后。

当我尝试使用从 CSV 中提取的数据时,我收到每个偏移量(0 到 907)的未定义偏移量通知。 (这可能意味着我没有成功提取数据,但我已经筋疲力尽,希望得到一些帮助)

有谁知道我做错了什么?

$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");

CSV 文件是标准的逗号分隔文件,因此我认为没有任何理由在此处发布该数据。

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.

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

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

发布评论

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

评论(2

羞稚 2024-12-08 10:05:38

语句

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

fgetcsv 将以包含元素的数组形式返回一行上线了;

因此,以下内容是错误的,需要在您迭代 CSV 中的单行时删除

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

因此,修改后,读取循环应该(我认为)如下所示:

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

在使用后,例如 print_r($one) 用于调试查看数组。

为了输出它,我很大程度上依赖于对您想要实现的目标的猜测,因为您正在输出到 db2.csv,但没有逗号(用于分隔)。但是尝试类似下面的方法

/*
 * 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]);

}

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]);

}
廻憶裏菂餘溫 2024-12-08 10:05:38

我和你一样,正在寻找解决方案......
事实上,它非常简单和恶魔:fgetcsv() 在读取末尾添加一个带有一个 NULL 值的数组。因此,$lines[1]会产生错误,因为最后添加的数组只有一个值。

只需计算 $lines 中的列数,如下所示:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文