I have an issue I cannot seem to fix. I have a function that takes a file and converts it to an array using the first row as the keys:
function parseCSVToArray($filePath)
{
$csvData = [];
if (($handle = fopen($filePath, "r")) !== false) {
$keys = fgetcsv($handle); // Get the first row as keys
while (($data = fgetcsv($handle)) !== false) {
$rowData = array();
foreach ($keys as $index => $key) {
$rowData[$key] = $data[$index] ?? ''; // Assign each value to its corresponding key
}
$csvData[] = $rowData;
}
fclose($handle);
}
return $csvData;
}
Everything works as normal and creates the array as expected:
$getTheRecords = parseCSVToArray('Data/records.csv');
// File contents
record,subdomain,hub_knows,domain,type,value,action,rationale
mail.sub.domain.com.,sub.domain.com,Hub knows about this,domain.com,CNAME,dispatch.domain.com.,DELETE,Dispatch links can go
Array
(
[record] => mail.sub.domain.com
[subdomain] => sub.domain.com
[hub_knows] => Hub knows about this
[domain] => domain.com
[type] => CNAME
[value] => dispatch.domain.com.
[action] => DELETE
[rationale] => Dispatch links can go
)
Now the issue is when I go to use or print the data. When I loop through the array using:
foreach($getTheRecords as $element)
{
echo "<div style='margin-bottom: 20px'>";
echo($element['subdomain']); // This will print the subdomain as expected.
echo "</div>";
}
If I change 'subdomain' to 'record' it prints nothing. However, every other 'key' prints the results just fine.
Thank you in advance for your help!
I have tried changing the name of the first key to 'mainrecord' or anything and it still will not print out.
Iside loop var_dmup():
array(8) {
["record"]=>
string(31) "mail.0lemonade.starchapter.com."
["subdomain"]=>
string(25) "0lemonade.starchapter.com"
["hub_knows"]=>
string(20) "Hub knows about this"
["domain"]=>
string(17) "scdomaintest3.com"
["type"]=>
string(5) "CNAME"
["value"]=>
string(22) "dispatch.scnetops.com."
["action"]=>
string(6) "DELETE"
["rationale"]=>
string(21) "Dispatch links can go"
}