1
votes

I have a CSV file to make some MySQL variables reader-friendly, where the first item in the row is the MySQL field name and the second is the user-friendly name to be filled in a field, like so:

Hymn1,First Hymn,
Hymn2,Second Hymn,
date,Date,
C2W,Call To Worship,

I will then create separate lists so people in the church can enter data that's relevant to them - the director of music will enter the names, authors, and lyrics to the anthem and offertory and the like. I will populate that list with the MySQL variable names and generate the page from that.

I want to convert that CSV into an array so I can generate a page that will echo the field names in user-readable ways for field labels.

I think part of my problem is that I have the CSV file sideways. Most answers assume the first line is the field name and the rest are the contents, but that's not the case here.

If this was Python, I would just zip them into a dictionary, but in PHP apparently you use associative arrays.


I opened the CSV and put the results in a string called $keys and echoed the string to verify it appeared as above, which it does.

Then I declared the array, got rid of the newline and replaced it with a second comma, then replaced the second comma with a pipe:

$assoc = array();
$keys = str_replace("\r", ",", $keys);
$keys = str_replace(",,", "|", $keys);

After that, I exploded the $keys string into a new variable with the pipe as the delimiter. So far, so good.

$newvar = explode("|", $keys);

Finally, I exploded each of the comma-separated items in each key in the array and associated them with each other in a foreach loop.

foreach($newvar as $iterator) {
    $mini = explode(",", $iterator);
    $mini[0] = ltrim($mini[0], "\n");
    $assoc[$mini[0]] = $mini[1]; }

When I did a print_r on my $assoc array, it gave me everything I'd hoped for. From source view:

Array
(
   [Hymn1] => First Hymn
   [Hymn2] => Second Hymn
   [date] => Date
   [C2W] => Call to Worship
)

But - and this is the problem part - when I tried to access individual elements in the array with echo $assoc['Hymn1']; or echo $assoc->Hymn1;I got nothing. I could echo "Hello World" and it would show up, but nothing from that.

I have also tried str_getcsv, but it only creates an array of each comma-separated item, or a similar array to this one that ultimately prints nothing when I try to echo each item.

I have googled, I have read dozens of answers on StackOverflow, including questions suggested by the page. I've been at this for hours and I'm ready to pull my hair out. I have read tutorials, I have tried creating a simple associative array and echoing it to make sure I'm doing it right (I am), and it all leads back to one answer: THIS SHOULD WORK, but it doesn't. Any ideas?

1

1 Answers

1
votes

There are 2 ways you can access the data, with either the field name or the user friendly version.

This code reads the file and creates two arrays as it's reading the file. $variables is uses the Hymn1 values as the key, $names uses the First Hymn values as the keys.

$variables = [];
$names= [];
$fh = fopen($fileName, "r");
while ( $row = fgetcsv($fh))    {
    $variables [$row[0]] = $row[1];
    $names [$row[1]] = $row[0];
}
fclose($fh);

print_r($variables);
print_r($names);