0

I want to get the first line of my Foo.csv as an array.

Foo.csv:

I, Like, Chocolate
And, Also, Milk

I tried

//$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"

I would like to end up with an array like this:

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

1 Answer 1

2

You can probably accomplish this more easily using fgetcsv(). Take a look at the documentation and corresponding example, then maybe use something like this (tested):

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

fclose($file);
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. I just checked. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.