用PHP计算文件中有多少行

use*_*195 1 php csv include

我是 PHP 新手,我真的需要你的帮助。我有一个这种形式的 CSV 文件(名为 *"Test.csv"):

"ID";"Nom de famille";"Prénom";"Age";"Téléphone mobile";"Téléphone";"Téléphone 2";"Fax";"Adresse de messagerie";"Commentaires"
Run Code Online (Sandbox Code Playgroud)

我需要 PHP 代码可以计算特定 CSV 文件中的行数,并将每行的“年龄”字段存储在数组中。

Ja͢*_*͢ck 5

最强大的解决方案,我能想到的通过记录只是阅读文件记录,因为CSV数据可能包含换行符的值:

$ages = array(); $records = 0;
$f = fopen('data.csv', 'rt');
while (($row = fgetcsv($f, 4096, ';')) !== false) {
    // skip first record and empty ones
    if ($records > 0 && isset($row[3])) {
        $ages[] = $row[3]; // age is in fourth column
    }
    ++$records;
}
fclose($f);

// * $ages contains an array of all ages
// * $records contains the number of csv data records in the file 
//   which is not necessarily the same as lines
// * count($ages) contains the number of non-empty records)
Run Code Online (Sandbox Code Playgroud)