There are 2 type for read CSV file using PHP script.
Method 1 ::
Using this can read CSV line by line, can get any number of cell value
$line = 1;
$csv = array(); //new array.
if (($handle = fopen("test.csv", "r")) !== FALSE) { // Read csv file
while (($obj = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($obj);
echo "<label> $num fields in line $row: <br /></label>\n";
$line++;
for ($i=0; $i < $num; $i++) {
echo $obj[$i] . "<br />\n";
}
$csv[] = $obj;
}
fclose($handle);
}
echo $csv[2][1]; //prints the 3th row, second column.
If want to get Array of the CSV then use Method 2 here,
PHP
Method 1 ::
Using this can read CSV line by line, can get any number of cell value
$line = 1;
$csv = array(); //new array.
if (($handle = fopen("test.csv", "r")) !== FALSE) { // Read csv file
while (($obj = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($obj);
echo "<label> $num fields in line $row: <br /></label>\n";
$line++;
for ($i=0; $i < $num; $i++) {
echo $obj[$i] . "<br />\n";
}
$csv[] = $obj;
}
fclose($handle);
}
echo $csv[2][1]; //prints the 3th row, second column.
If want to get Array of the CSV then use Method 2 here,
No comments:
Post a Comment