php - 读取 CSV 文件的限制
问题描述
我有一个包含多行的 CSV 文件,我想在 php 中只恢复第 1 列值等于 27 的数据,我该怎么做?
这就是我已经做过的:
<?php
require(dirname(__FILE__) . '/config/config.inc.php');
$fileDir = 'stock.csv';
$fileCsv = file_get_contents($fileDir);
echo $fileCsv;
我的 CSV 文件有 3 列:
| number | ref | stock |
| 19 | 135 | 10 |
| 27 | 656 | 20 |
谢谢你。
解决方案
正如 RiggsFolly 指出的那样;fgetscsv() 是您最好的工具。
$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);
推荐阅读
- python - 保存每次迭代的结果
- sql - 我如何像 TFS 一样进行 MS SQL 脚本版本控制?
- windows - 如何解决控制台窗口阻塞?
- php - 在 Woocommerce 管理员订单预览中显示自定义数据
- node.js - 通过 NodeJS 中的 base64 编码处理图像/jpeg 响应并在 UI 上显示
- javascript - 转换可读流以将其保存为本地文件
- javascript - Applet 类未加载到 JSP 中
- java - 如何检查字符串是否包含集合中的字符串之一
- ios - Objective C - 在视图控制器中调用方法,该方法已经在应用程序委托的导航顶部
- batch-processing - 在 apache Beam 中根据批量大小获取记录