警告:fputcsv()期望参数2为数组,布尔值。写入新的csv文件时。的PHP [英] warning: fputcsv() expects parameter 2 to be array, boolean. when writing to new csv file. php

查看:409
本文介绍了警告:fputcsv()期望参数2为数组,布尔值。写入新的csv文件时。的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为employee_data.csv的CSV文件。它包含格式如下的员工记录:

I have a CSV file called employee_data.csv. It contains employee records formatted like so:

JANE WILLIAMS,6/8/1998,55846874E,4323
PETER JONES,15/01/1982,56897547Q,1234
JAMES O'BRIEN,09/05/2001,25689514W,3432

我要删除csv文件中的选定行。为此,我只需将不想删除的csv文件中的两行复制到new_employee_data.csv文件中,然后删除旧行。

I want to delete a selected row within the csv file. To achieve this, I will simply copy the 2 rows within the csv file that I do not want to delete to a new_employee_data.csv file and delete the old one.

<?php
$dataSrc = "persistence/employee_data.csv";
$dataDest = "persistence/new_employee_data.csv";

$dataFile = fopen($dataSrc, "r") or die("Unable to open file!");
$outFile = fopen($dataDest, "w") or die("Unable to open file!");

$i=0;  //index for the array
    while(!feof($dataFile)) {
    $csv = fgetcsv($dataFile); //read a line from the CSV file
    //$csv = [ANE WILLIAMS,6/8/1998,55846874E,4323];
    //add check to remove row
    print_r($csv);
        if($csv[2] == '55846874E') continue; //skip to next itteration

        fputcsv($outFile, $csv);
}

fclose($dataFile);
fclose($outFile);
?>

上面的代码接受 $ dataFile 并将其逐行写入 $ outFile ,如果第3列='55846874E',它将跳过写该行。 csv数组包含employee_data.csv文件中的行。

The code above takes the contents of $dataFile and writes it to $outFile line by line, if the 3rd column = '55846874E' it will skip writing that line. The csv array contains the rows within the employee_data.csv file.

$ csv数组中的元素如下。

The elements in the $csv array are as follows.

Array ( [0] => JANE WILLIAMS [1] => 6/8/1998 [2] => 55846874E [3] => 4321 )
Array ( [0] => PETER JONES [1] => 15/01/1982 [2] => 56897547Q [3] => 1234 ) 
Array ( [0] => JAMES O'BRIEN [1] => 09/05/2001 [2] => 25689514W [3] => 8475 ) 

它将删除文件的第一行- JANE WILLIAMS,6/8 / 1998,55846874E,4323

It removes the first row of the file - JANE WILLIAMS,6/8/1998,55846874E,4323

现在在new_employee_data.csv中是两个未删除的记录。

Now in the new_employee_data.csv is the two undeleted records.

"PETER JONES",15/01/1982,56897547Q,1234
"JAMES O'BRIEN",09/05/2001,25689514W,8475

这正是我要执行的操作,但是在浏览器中运行它时收到此警告:

This is exactly what I want it to do however I received this warning when I run it in the browser:


fputcsv()期望参数2为数组,第25行给出布尔值

fputcsv() expects parameter 2 to be array, boolean given in line 25

fputcsv($ outFile,$ csv); 有问题,我已经不知道为什么,关于如何解决此问题的任何建议?

It's having a problem with fputcsv($outFile, $csv); and I've no idea why, any suggestions of how to fix this?

推荐答案

我会更改while循环,因此而不是

I would change the while loop So instead of

while(!feof($dataFile)) {
    $csv = fgetcsv($dataFile); 

像这样

while(false !== ($csv = fgetcsv($dataFile))){

您可以在PHP网站此处

可能发生的情况是文件末尾有额外的返回,因此 feof 没有抓住它,然后您为 fgetcsv 得到布尔值 false 。例如这样(其中\n是换行):

What probably happens is there is a extra return at the end of the file, so the feof doesn't catch it, and then you get boolean false for the fgetcsv. For example like this (where \n is a new line):

JANE WILLIAMS,6/8/1998,55846874E,4323\n
PETER JONES,15/01/1982,56897547Q,1234\n
JAMES O'BRIEN,09/05/2001,25689514W,3432\n
\n
\eof

因此我们可以将它们组合在一起(因此您将不需要下面的行while循环)并仅从我们执行循环条件的同一位置获取数据,这样,当它返回false时,它将删除循环。重要的是要小心其中的 = 数,因为一个是赋值,而!== 是严格的类型比较。因此,我们可以将其分解为英文,然后用英语怎么说。

So we can combine these (so you wont need the line under the while loop) and just get the data from the same place we do the loop condition, this way when it returns false it will just drop the loop. It's important to be careful with the number of = in this as a single one is assignment and the !== is strict type comparison. So we can break this down a bit and in English what this says.


  • 拉一行并用fgetcsv对其进行处理,将$ csv设置为值,括号获得优先级

  • 如果$ csv为布尔值false,则循环条件为false并结束。因此,基本上如果false(boolean)不等于 fgetcsv($ dataFile)的结果,则为true,否则为false

  • pull a row and process it with fgetcsv setting $csv to it's value, parentheses get priority
  • if $csv is boolean false then the loop condition is false an it ends. So basically if false(boolean) not equal to the result of fgetcsv($dataFile) then it's true otherwise it's false.

它的工作原理基本上与此相同

It would work basically the same like this

while($csv = fgetcsv($dataFile)){

我更喜欢长手版本,因为更容易看出我们正在分配而不是进行比较。例如,您可以浏览上述版本,然后认为它应该是 == 而不是 = ,因此第一个版本只是使这一点更加明显。出于相同的原因,将 false 放在左侧基本上是出于相同的原因(并且由于它本质上是一个常数,因此将其放在左侧可以避免出现<$ c $以下的错误c> false = $ csv 无法正常工作。)

I tend to like the long hand version, because it's easier to see that we are assigning and not comparing. For example you could glance at the above version and then think it should be == instead of = so the first version just makes that a bit more obvious. Putting the false on the left hand side is basically done for the same reason (and because it's essentially a constant, so putting it on the left avoids mistakes like below false = $csv wont work).

= 替换为条件实际上可能是更难发现的错误之一,因为它是完全合法的。因此,这是在进行比较时将函数调用和常量放在左侧的专家提示。

Misplacing an = in a condition can actually be one of the harder bugs to figure out, because it's completely legal. So that is kind of a "pro tip" to put function calls and constants on the left when doing comparison.

希望有帮助!

这篇关于警告:fputcsv()期望参数2为数组,布尔值。写入新的csv文件时。的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆