我在CSV文件中有数据。我需要读取文件并将数据放入数组中。我的CSV文件中有5列,如下所示:
CAPRICCIOSA | Tomato, Cheese, Mushrooms | $8.00 | $12.00 | $15.00
MARGHERITA | Tomato, Cheese, Oregano | $7.00 | $11.00 | $13.00explode()成数组?如果我使用逗号将它分解成数组,它将变成CAPRICCIOA - Tomato - Cheese,因为我的文本已经有了一个逗号。我尝试了以下几点:
$file="dish.csv" ;
$file=file_get_contents($file);
$array=explode(",", $file);发布于 2012-12-15 04:32:28
如何使用PHP函数: fgetcsv()
发布于 2012-12-15 04:33:36
您应该转到fgetcsv:
<?php
$row = 1;
if (($handle = fopen("test.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);
?>发布于 2012-12-15 09:23:47
试试这个:
$fn='dish.csv';
$file_array = file($fn);
foreach ($file_array as $line_number =>&$line)
{
//$line=utf8_encode($line);
$row=explode('|',$line);
echo 'Dish: '.$row[0].'<br>';
$ingredients=explode(',',$row[1]);
echo 'Ingredients:';
echo '<pre>';
print_r($ingredients);
echo '<br><br>';
echo '</pre>';
}https://stackoverflow.com/questions/13889305
复制相似问题