Reading csv file to array

I am incredibly new to php, so please carry me and help me learn. I have a CSV file whose length is 33 lines (including headers) and 4 columns. I want to read this data in an array to start sorting and manipulating it.

What is the best way to do this? Code snippets are the best way to find out how I can read, interpret, use, and then retake questions that I may have.

+3
source share
1 answer

Using fgetcsv returns an array from the line of the csv file. To see how it exploded, you run your returned array in the print_r () function. To see that as a pretty printed type, you can wrap it in tags<pre>

<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    echo "<pre>".print_r($data)." <br /></pre>";
}
fclose($handle);
}
?>

This should be a good start.

http://php.net/manual/en/function.fgetcsv.php

+5
source

Source: https://habr.com/ru/post/1777326/


All Articles