After 2+ hours looking through Stack Overflow for a solution, I'm now needing to ask for guidance. My inexperience may mean I just don't know how to "ask" the right question, but I haven't found anything similar to what I'm looking to do.
Basically, a user will supply a CSV file which will be uploaded server-side. It will ALWAYS be 4 columns. It may or may not include a column header. The user will notate (check box) whether a header exists. This flag will be used in my PHP script. If a header exists, I need to ignore it (delete) because I need to provide specific Key values and I can't control what the user places in the column header. So, either way, I'm going to end up with a CSV file that needs to be converted into an array with my specific Key values for each of the four columns.
All of my searches within Stack Overflow only bring up solutions that WANT to use column headers. In my case, I need to disregard the column header (when it is present) and supply specific Key values (e.g., f_name, l_name, position, position_area).
So far I've been successful in opening and displaying the information within the file:
$file = fopen('filename', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
I found this on Stack Overflow which USES a header and successfully places each column header as a Key value:
function parse_csv_file($csvfile) { // SO #1269562
$csv = Array();
$rowcount = 0;
if (($handle = fopen($csvfile, "r")) !== FALSE) {
$max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000;
$header = fgetcsv($handle, $max_line_length);
$header_colcount = count($header);
while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) {
$row_colcount = count($row);
if ($row_colcount == $header_colcount) {
$entry = array_combine($header, $row);
$csv[] = $entry;
}
else {
error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount");
return null;
}
$rowcount++;
}
//echo "Totally $rowcount rows found\n";
fclose($handle);
}
else {
error_log("csvreader: Could not read CSV \"$csvfile\"");
return null;
}
return $csv;
}
Even though I see what is being done in these two code examples, I truly don't know what would be the best way to: 1. Delete the CSV header if it exists; and 2. Assign specific Key values when I build the array
This is what I would like to end up with:
From this...
Jamison,Cummings,Logistics Analyst,Logistics Operations
Bryce,Downey,Courier Specialist,Major Accounts
Rich,Lane,Supervisor,Receiving Scheduling
To this ...
[0] => Array
(
[f_name] => Jamison
[l_name] => Cummings
[position] => Logistics Analyst
[position_area] => Logistics Operations
)
[1] => Array
(
[f_name] => Bryce
[l_name] => Downey
[position] => Courier Specialist
[position_area] => Major Accounts
)
[2] => Array
(
[f_name] => Rich
[l_name] => Lane
[position] => Supervisor
[position_area] => Receiving Scheduling
)
I would appreciate any direction you can provide. Thank you.