开发者

fgetcsv and "0" value

开发者 https://www.devze.com 2022-12-16 20:40 出处:网络
So I\'m using the PHP function fgetcsv to parse a .csv file开发者_开发问答 (comma separated, fields are enclosed by \"\") and import it to a MySQL. The problem is, a certain field is always empty, eve

So I'm using the PHP function fgetcsv to parse a .csv file开发者_开发问答 (comma separated, fields are enclosed by "") and import it to a MySQL. The problem is, a certain field is always empty, even though it should sometimes contain "0". So I'm guessing the function consider "" and "0" as null values, but it's quite problematic for me. Any ideas?

EDIT - The code :

$handle = fopen("/home/simon/projet-web/Quebec.csv", "r");

if ($handle)
{      
  while($line = fgetcsv($handle, 0, ",", "\""))
  {
    if ($line[55] === "0") 
      echo $line[55] . " / " . $line[4] . "<br />";
  [...]


Use Identical Comparision Operator.


You should be able to tell the difference between a zero entry and empty field using the following...

if($currentRow[$currentIndex] === 0)

...to check for the presence of a zero. (The same logic applies when using functions that return either a zero or false, such as strpos - in that instance, you'd check for !== false, etc.)

As Crozin says, this is the identical comparison operator, which checks for type as well as value equivalence.

0

精彩评论

暂无评论...
验证码 换一张
取 消