0

I have the following csv file:

9 carat gold,11.87
18 carat gold,23.73
Silver,0.49
Platinum,27.52

I would like to put this in a Multidimensional Array but i have no idea how to.

Thanks for any help.

3 Answers 3

1

Try this code

$csv = file_get_contents($file);
$data = explode(PHP_EOL, $csv);
$arr = array();
foreach ($data as $entry) {
    $arr[] = str_getcsv($entry);
}
print_r($arr);

You can go through this link for more details about str_getcsv function

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

Comments

0

You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.

Example from PHP Manual:

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

Comments

0

You can use fgetcsv to read a csv file open with fopen.

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.