473,467 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fgetcsv() Skip empty rows

code green
1,726 Recognized Expert Top Contributor
I am trying to skip empty rows in a csv file
The manual states
Note:
A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.
But none of these tests seem to work.
Anybody know why
[PHP]while(($data = fgetcsv($this->handle)) !== false)
{
if(!is_null($data[0])) #no
if(!empty($data)) #no
if(!empty($data[0])) #no
{[/PHP] And variations thereof
Aug 13 '08 #1
2 16831
Atli
5,058 Recognized Expert Expert
Hi.

You are testing to see if there is a null, right?
That is, if the field is null, you want the if to evaluate true?
If so, why do you flip the result of the if statement?

Testing this on my own, the empty function worked perfectly.
Even a simple boolean check against the empty array element worked.

The is_null function doesn't work, however. This would be due to the fact that it apparently only checks for a variable set with the NULL constant, which the fgetcsv doesn't do for empty cells.

All of this worked for me:
Expand|Select|Wrap|Line Numbers
  1. if($_v == null)
  2. if(empty($_v))
  3. if(!$_v);
  4.  
If that isn't working for you, could there be a problem with the CSV data?

Edit:
Ok, so I kind of missed the point there. Was thinking about empty cells :P

But, with completely empty rows, these three still worked for me:
Expand|Select|Wrap|Line Numbers
  1. if($data[0] != null) {
  2.   # Show row
  3. }
  4. if($data[0]) {
  5.   # Show row
  6. }
  7. if(!empty($data[0])) {
  8.   # Show row
  9. }
  10.  
Aug 14 '08 #2
code green
1,726 Recognized Expert Top Contributor
Thanks for the Edit. You nearly lost me there Atli.
Did some testing. These are the results

[PHP]FAILED
if(!is_null($data[0]))
if(!empty($data))
if(($data))
if(!is_null($data))
if($data != null)

WORKED
if(!empty($data[0]))
if($data[0] != null)
if($data[0])
if(!empty($data[0])) [/PHP]
Conclusion
Looks pretty obvious that the first element must be tested.
The NULL value in element 0 means an empty element but a non-empty array.
I didn't realise this was the case.
Don't know whats going on with is_null

I suppose if($data[0] != null) is the safest to use just in case the first column of a row is empty.

ADDITION
[PHP] if($data[0] !== null)
if(!($data[0] === null))[/PHP] Don't work
Aug 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joe Randstein | last post by:
Hi! I get CSV-data from a post-request. How can I work on that? Currently I save the data to a temp-file, then read it back with fgetcsv and then delete the temp-file. That works, but I don't...
3
by: puzzlecracker | last post by:
I want to read lines and skip blank lines: would this work considering the lines can contain tabs, spaces, etc.? file.in: ------ line1 line2
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
59
by: AK | last post by:
I tried to google "skip scan DB2" but came up with nothing. Does DB2 have the feature under a different name?
2
by: Stephen Preston | last post by:
I have an xls worksheet I wish to export as a text or csv file to import with fgetcsv() The 'save as' function on excell lets me save as a comma separated value or tab delimited text files....
0
by: tjonsek | last post by:
I am working with directories in PHP for the first time. I have code that I've changed multiple times to try different things. I would think this is pretty standard fare so I'm not sure why I can't...
0
by: et | last post by:
fgetcsv() do unwanted "left trim" on fields started with locale characters (code page win-1250). Problem persist only if local character is first character in field for example, let say that C...
5
by: mantrid | last post by:
I am using fopen() and fgetcsv() to open an excel file and extract data for upload to mysql database. The doesnt seem to be a parameter in either of these functions for ommitting the first row of...
3
by: jrrdnx | last post by:
I have a form set up to allow someone to upload a .csv and populate its data into a database using the fgetcsv() function. All of the code works fine because everything in the .csv file is populated...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.