PHP fgets()到数组
我想获取Foo.csv的第一行作为数组。 Foo.csv:PHP fgets()到数组,php,arrays,csv,fgets,Php,Arrays,Csv,Fgets,我想获取Foo.csv的第一行作为数组。 Foo.csv: I, Like, Chocolate And, Also, Milk 我试过了 //$foo is Foo.csv $file = fopen($foo, "r") //First attempt $fgetsFile = fgets($file) //Other way $streamlineFile = stream_get_line($file, 10000, "\n"); fclose($file) var_dump($fg
I, Like, Chocolate
And, Also, Milk
我试过了
//$foo is Foo.csv
$file = fopen($foo, "r")
//First attempt
$fgetsFile = fgets($file)
//Other way
$streamlineFile = stream_get_line($file, 10000, "\n");
fclose($file)
var_dump($fgetsFile) // (String) "I", "Like", "Chocolate"
var_dump($streamlineFile) // (array) [0] => (string) "I", "Like", "Chocolate"
我希望最终得到这样一个数组:
array([0] => "I", [1] => "Like", [2] => "Chocolate)
使用fgetcsv()可能更容易实现这一点。看一看和相应的例子,然后可能使用类似的东西(测试):
的确我刚查过。谢谢
if(($file = fopen($foo, "r")) !== false){
if(($data = fgetcsv($file)) !== false){
var_dump($data);
}
}
fclose($file);