dongyuan9149
2015-05-07 13:44
浏览 143
已采纳

使用特定列中的PHP计算CSV文件中的行数

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);

图片转代码服务由CSDN问答提供 功能建议

我的php正在计算完整csv文档中的行数,但试图得到一个 特定列中包含数据的行数。 这可能用PHP吗?

  $ fp = file('test.csv'); 
echo count($ fp); 
    
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjianao0592 2015-05-07 14:41
    已采纳

    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;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题