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

1 Answer 1

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;

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.