最简单的方法是使用php读取csv文件,然后选择一个特定的值 [英] Simplest way to read a csv file using php, and then select one specific value
问题描述
预先感谢您的时间/帮助。我是新手学习php,所以请记住这一点。
Thank you in advance for your time/help. I'm a newbie learning php, so please keep that in mind.
第一个问题
我需要一个读取csv文件的php脚本。
1st Question I need a php script that reads a csv file.
第二个问题
如何回显该文件中的特定单元格(行和列/列)
2nd Question How do I echo a specific cell (row and row/column) in that file
我发现这个脚本在类似的回复,脚本读取整个文件完美,但我只想选择一个特定的单元格/值在该csv文件。那么如何回显一个特定的行或一个行和列。
I found this script on a similar reply, the script reads the whole file perfectly, but I only want to select a specific cell/value on that csv file. So how do I echo a specific row or a row and column.
$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
推荐答案
case,最好的方法是在php中做一个多维数组。
If you need to select several cases, the best way is to make a multi-dimensional array in php. Otherwise, just throw a few counters into your while loops and done.
$row = 1;
$mycsvfile = array(); //define the main array.
if (($handle = fopen("1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
$mycsvfile[] = $data; //add the row to the main array.
}
fclose($handle);
}
echo $mycsvfile[3][1]; //prints the 4th row, second column.
这篇关于最简单的方法是使用php读取csv文件,然后选择一个特定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!