For example I wanted to do something like
php sample.php [csv file] [column name]
it should display all data based on the specified column name. My code below just gets the first column but I wanted it to be a variable based on column name
CURRENT CODE
function getCSV($csv){
$file = fopen($csv, 'r');
while (!feof($file) ) {
$lines[] = fgetcsv($file, 1024);
}
fclose($file);
return $lines;
}
$csv = getCSV($argv[1]);
foreach ($csv as $values) {
$row1[] = $values[0];
}
print_r($row1);
SAMPLE CSV
ID,Name
1,Amy
2,Ana
3,Ava
EXPECTED OUTPUT
php sample.php sample.csv ID
1
2
3
php sample.php sample.csv Name
Amy
Ana
Ava
How do I do this?
explodecan be tricky if you're relying on consistent delimiters, and it'll fail on delimiters embedded in values.print_r($csv);after you set$csvfrom thegetCSV()function. See what it looks like at that point, and that might help you loop over it.