Skip to content
Advertisement

Is it possible to count how many rows have an empty specified ‘cell’ or column in a csv?

I have a changing csv file that looks something like the following. I am trying to find out how I can count how many jobs have not been completed yet (have an empty field) CSV example:

JavaScript

In this example, I am looking to return ‘3’. (out of the 6 lines, 3 lines show row 4 as being empty)

This is what I have so far:

JavaScript

Advertisement

Answer

I’ve done 2 versions of the code, the first one is based on what you already have, the second (IMHO) is a lot simpler.

Based on what you already have, the difference is that you currently filter on an entire row, so it will only count empty rows. This uses array_column() to extract the 4th column and then filters that. This also gives the number of filled rows, so I subtract it from the total rows…

JavaScript

This uses a straight read CSV with fopen() and fgetcsv(). At each row it counts if the column is empty, this reduces the use of several array_ functions and makes it so much easier to read…

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement