Read data for all rows in a CSV file at once
Copy Code code as follows:
<?php
$file = fopen (' windows_2011_s.csv ', ' R ');
while ($data = Fgetcsv ($file)) {//read one line in CSV every time
Print_r ($data); This is an array, to obtain each data, access to the array subscript can
$goods _list[] = $data;
}
Print_r ($goods _list);
/* foreach ($goods _list as $arr) {
if ($arr [0]!= "") {
echo $arr [0]. " <br> ";
}
} */
echo $goods _list[2][0];
Fclose ($file);
?>
read a row of data in a CSV file
Copy Code code as follows:
<?php
function Get_file_line ($file _name, $line) {
$n = 0;
$handle = fopen ($file _name, ' R ');
if ($handle) {
while (!feof ($handle)) {
+ + $n;
$out = Fgets ($handle, 4096);
if ($line = = $n) break;
}
Fclose ($handle);
}
if ($line = = $n) return $out;
return false;
}
Echo get_file_line ("Windows_2011_s.csv", 10);
?>
read CSV file number of lines (line range)
Copy Code code as follows:
<?php
function Get_file_line ($file _name, $line _star, $line _end) {
$n = 0;
$handle = fopen ($file _name, "R");
if ($handle) {
while (!feof ($handle)) {
+ + $n;
$out = Fgets ($handle, 4096);
if ($line _star <= $n) {
$ling [] = $out;
}
if ($line _end = = $n) break;
}
Fclose ($handle);
}
if ($line _end== $n) return $ling;
return false;
}
$AA = Get_file_line ("Windows_2011_s.csv", 11, 20); Lines from line 11th to 20th
foreach ($aa as $BB) {
echo $BB. " <br> ";
}
?>
In addition to find two ways from the Internet (no test, do not know how to make)
Copy Code code as follows:
?
$handle =fopen ("1.csv", "R");
while (!feof ($handle)) {
$buffer =fgetss ($handle, 2048);
$data =explode (",", $buffer);
$num =count ($data);
for ($i =0; $i < $num; $i + +) {
Print_r ($data);
}
}
?>
Copy Code code as follows:
?
$handle =fopen ("1.csv", "R");
$row = 1;
while ($data =fgetcsv ($handle, 1000, ",")) {
$num =count ($data);
for ($i =0; $i < $num; $i + +) {
echo $data [$i];
}
$row + +;
}
?>