我有一个csv文件,看起来像这样
$lines[0] = "text, with commas", "another text", 123, "text",5;
$lines[1] = "some without commas", "another text", 123, "text";
$lines[2] = "some text with commas or no",, 123, "text";
Run Code Online (Sandbox Code Playgroud)
我想要一张桌子:
$t[0] = array("text, with commas", "another text", "123", "text","5");
$t[1] = array("some without commas", "another text", "123", "text");
$t[2] = array("some text, with comma,s or no", NULL , "123", "text");
Run Code Online (Sandbox Code Playgroud)
如果我使用split($lines[0],",")我会得到"text" ,"with commas" ...
有没有任何优雅的方式来做到这一点?
我有一个CSV文件:
Test1,One line
Test2,"Two lines
Hello"
Test3,One line
Run Code Online (Sandbox Code Playgroud)
如您所见,其中一列具有一个用新行分隔的值.
要将此CSV文件解析为数组,我运行:
$csvArray = array();
$csvData = file_get_contents('file.csv');
$lines = explode(PHP_EOL, $csvData);
foreach ($lines as $line) {
$csvArray[] = str_getcsv($line);
}
// print_r($csvArray);
Run Code Online (Sandbox Code Playgroud)
它适用于一个问题.它将值中的新行读作新行,这完全不正确.
如何才能正确读取多行值?
编辑:这个问题集中在新线上.
我有以下内容:
$counter = 1;
while($row= mysql_fetch_assoc($result)) {
$counter2 = $counter++;
echo($counter2 . $row['foo']);
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来获得1,2,3等每个结果或这是最好的方法?
谢谢
如果我有一个文本文件:
Ramon,19,Libra
Renata,25,Aries
Roxy,52,Leo
Run Code Online (Sandbox Code Playgroud)
如果我要创建一个数组:
print_r(explode(",", $str));
Run Code Online (Sandbox Code Playgroud)
我如何解释换行符?