I'm currently writing a script to display the fields from a large CSV and display them in arrays. Currently I have a working script as below:
<?php
function csvToArray($csvFile){
$file_to_read = fopen($csvFile, 'r');
while (!feof($file_to_read) ) {
$lines[] = fgetcsv($file_to_read, 1000, ',');
}
fclose($file_to_read);
return $lines;
}
$csvFile = 'example.csv';
$csv = csvToArray($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>
This returns every line of the CSV into arrays.
However, what I want to do next is have only specific fields be returned in these arrays, and have said fields be named. So, for example, right now the output looks something like:
(0) =>
(1) => £100
(2)=> Cancelled
(3) =>
(4) =>
But what I want to output would be something like:
(Subscription Cost) => £100
(Subscription Status) => Cancelled
Can someone give me a hand with how to achieve this? I've never used any CSV functions with PHP before so I'm starting from scratch currently with my knowledge.