2

I want to count total number of rows with data in csv. I did following:

<?php echo count(file('example.csv',FILE_IGNORE_NEW_LINES |` `FILE_SKIP_EMPTY_LINES)); ?>

Here, if all the values are only in first column then output is correct. eg. Row1Col1='a' & Row4Col1='b' o/p=2 correct because data in rows are

Row1->'a',Row2->'',Row3->'',Row4->'b'

But if values are in different column then get wrong output. eg. Ro1Col3='a' & Row4Col1='b' o/p=4 wrong (want correct output is 2) because data in rows are

Row1->'a', Row2->',,' Row3->',,' Row4->'b'.

Thanks, Jaydeep

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77

1 Answers1

1

You could try the PHP function function.str_getcsv

You can use function.array_filter to remove any null elements within your CSV.

<?php

    $csv_complete_contents = array_map('str_getcsv', file('example.csv'));
    $csv_complete_filtered = array_filter(array_map('array_filter', $csv_complete_contents));
    $csv_num_rows = count($csv_complete_filtered);

    echo $csv_num_rows;

?>
Community
  • 1
  • 1
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78