0

I want to convert a csv file with data like this in a multidimensional array.

1;name1;date1
2;name2;date2

The name of the file is vervangingen.csv. And I want the array to be like this:

Array (   

 [0] => Array
        (
            [0] => 1
            [1] => name1
            [2] => date1
        )

[1] => Array
    (
        [0] => 2
        [1] => name2
        [2] => date1

    )

)

Currently I have this:

    $csv = array();
    $file = fopen('vervangingen.csv', 'r');

    while (($result = fgetcsv($file)) !== false)
    {
        $csv[] = $result;
    }

    fclose($file);

    echo '<pre>';
    print_r($csv);
    echo '</pre>';

But this makes the array look like this. I know there exists something like explode, but I don't know how exactly I would use it here. Could anyone help me maybe?

Array
(
    [0] => Array
        (
            [0] => 1;name1;date1
        )

    [1] => Array
        (
            [0] => 2;name2;date2
        )

)

1 Answer 1

4

You should set correct delimiter:

fgetcsv($file, 0, ';')

as described in the PHP docs

Sign up to request clarification or add additional context in comments.

Comments

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.