I'd like to populate my data array with the contents of a CSV file using PHP.
My current code:
<script src="http://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
<?php
$file = fopen('food.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
fclose($file);
?>
<script type="text/javascript">
var data = <?php echo json_encode($line); ?>;
document.write(data);
</script>
However, when I run this, I get the following output:
Array
(
[0] => McDonalds
[1] => Fast Food
[2] => London
)
Array
(
[0] => Marios
[1] => Italian
[2] => Manchester
)
<script type="text/javascript">
var data = false;
document.write(data);
</script>
I'm guessing the $line variable is my issue here.
My food.csv file:
McDonalds,Fast Food,London
Marios,Italian,Manchester
The plan is to incorporate the data array into something like the demo below:
JavaScript Object Notationformat.