2014-12-05 104 views
0

我想將我的Foo.csv的第一行作爲數組。PHP fgets()數組

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($fgetsFile) // (String) "I", "Like", "Chocolate" 
var_dump($streamlineFile) // (array) [0] => (string) "I", "Like", "Chocolate" 

我想用這樣一個數組,結束了:

array([0] => "I", [1] => "Like", [2] => "Chocolate) 

回答

2

你或許可以做到這一點更輕鬆使用fgetcsv()。看看the documentation和相應的例子,那麼也許使用這樣的(測試):

if(($file = fopen($foo, "r")) !== false){ 
    if(($data = fgetcsv($file)) !== false){ 
     var_dump($data); 
    } 
} 

fclose($file); 
+0

確實。我剛剛檢查過。謝謝。 – Wistar 2014-12-05 22:22:59