douhuang3833 2018-06-05 09:40
浏览 59
已采纳

读取CSV文件的限制

I have a CSV file with several lines and I would like in php only recover the data whose column 1 the value is equal to 27, how can I do that?

That's what I already did:

<?php

require(dirname(__FILE__) . '/config/config.inc.php');

$fileDir = 'stock.csv';

$fileCsv = file_get_contents($fileDir);

echo $fileCsv;

My CSV file has 3 columns:

| number | ref | stock |
|   19   | 135 |   10  |
|   27   | 656 |   20  |

Thank you.

  • 写回答

2条回答 默认 最新

  • dou44364983 2018-06-05 09:47
    关注

    As RiggsFolly pointed out; fgetscsv() is your best tool for this.

    $fileDir = 'stock.csv';
    //open the file for reading
    $fileCsv = fopen($fileDir,"r");
    //initialize our output array
    $outputData = array();
    //define the line length required for fgetcsv
    $lineLength = 100;
    //loop the contents of csv and split by `,`
    while($row = fgetcsv($fileCsv,$lineLength,',')){
       //does our first column equal 27?
        if($row[0] == 27){
            //store the full row into our output array
            $outputData[] = $row;
        }
    }
    //output
    print_r($outputData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信