jibly
jibly

Reputation: 115

Count number of rows in a CSV file with PHP in a particular column

I've got my php working to count the number of rows in the full csv document, but trying to get a number of rows that have data in them in a particular column. Is this possible with PHP?

$fp = file('test.csv');
echo count($fp);

Upvotes: 0

Views: 4121

Answers (1)

Yasen Zhelev
Yasen Zhelev

Reputation: 4045

You can use fgetcsv function and check in every row the data.

For example if you want to check how many rows have data in the second column, run that.

$data_found = 0;
$handle = fopen("test.csv", "r");
while ($data = fgetcsv($handle))
{
  if ($data[1] != '')
  {
    // check the data specifically in the second column
    $data_found ++;
  }
}
echo 'Rows with data in the second column:'.$data_found;

Upvotes: 3

Related Questions