Like you said in your title, fgetcsv is the way to go. It's pretty darn easy to use.
$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
fclose($file);
You'll want to put more error checking in there in case fopen() fails, but this works to read a CSV file line by line and parse the line into an array.
How to create an array from a CSV file using PHP and the fgetcsv function - Stack Overflow
csv file into an array in php
CSV file to PHP array converter - Stack Overflow
How do I convert a csv file into a php array?
\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
\n"; } } fclose($handle); } ?> They key thing to note here is that, every time you call fgetcsv it will retrieve another record (row) from the input file. If you just wanted to put the contents into a CSV file, you could probably just do something like this (I haven't actually run this code): More on reddit.com
How to use the
Convert CSV to PHP Array Online for free?
How to convert CSV to PHP online?
What is PHP Array format?
Like you said in your title, fgetcsv is the way to go. It's pretty darn easy to use.
$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
fclose($file);
You'll want to put more error checking in there in case fopen() fails, but this works to read a CSV file line by line and parse the line into an array.
I think the str_getcsv() syntax is much cleaner, it also doesn't require the CSV to be stored in the file system.
$csv = str_getcsv(file_get_contents('myCSVFile.csv'));
echo '<pre>';
print_r($csv);
echo '</pre>';
Or for a line by line solution:
$csv = array();
$lines = file('myCSVFile.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
echo '<pre>';
print_r($csv);
echo '</pre>';
Or for a line by line solution with no str_getcsv():
$csv = array();
$file = fopen('myCSVFile.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
echo '<pre>';
print_r($csv);
echo '</pre>';
ive got a csv file with numbers separated by commas but the end number doesnt. so it looks like this. (example not actual numbers)
1,2,3,4,5 6,7,8,9,10 11,12,13,14,15
ive been trying to get fgetcsv to work, and i get a weird looking array like this.
array(9) { [0]=> string(2) "17" [1]=> string(2) "42" [2]=> string(2) "15" [3]=> string(4) "1300" [4]=> string(4) "2830" [5]=> string(4) "1170" [6]=> string(1) "3" [7]=> string(1) "5" [8]=> string(1) "2" } array(9) { [0]=> string(2) "80" [1]=> string(2) "20" [2]=> string(2) "50" [3]=> string(3) "540" [4]=> string(4) "1160" [5]=> string(3) "745" [6]=> string(1) "5" [7]=> string(3) "150" [8]=> string(3) "200" } array(9) { [0]=> string(1) "4" [1]=> string(2) "68" [2]=> string(2) "90" [3]=> string(3) "900" [4]=> string(4) "5420" [5]=> string(5) "10000" [6]=> string(2) "40" [7]=> string(1) "7" [8]=> string(3) "190" }
how do i print just one element? like row 1 col 1. obv its row 0 col 0 in the index but i cant get it to work?
Look into fgetcsv()
<?php
$handle = fopen("test.csv", "r");
$result = Array();
fgetcsv($handle); //Removes the first line of headings in the csv
while($data = fgetcsv($handle)) {
$result[$data[0]] = $data[1];
}
print_r($result); //the result
?>
There are other ways to do it, but given your current code, just extract an array of the 1 values and index it by the 0 values.
unset($data[0]); //If needed to remove the header row
$data = array_column($data, 1, 0);
You may consider this as an alternate for the first step (not sure if FILE_IGNORE_NEW_LINES is absolutely necessary):
$data = array_map('str_getcsv', file('test.csv', FILE_IGNORE_NEW_LINES));