ScottD
ScottD

Reputation: 582

Send tab delimited txt values to PHP array

I have a tab delimited txt file, where I would like to retrieve certain data off of each line, and place that data into an array.

Here is what I have so far:

$file_handle = fopen("/file.txt", "r");
$list = "";
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 0, "\t");
    $list .= $line_of_text[10] . " - " . $line_of_text[9] . ": " . $line_of_text[6];
}
fclose($file_handle);

I then want to put $list into an array like this array($list). This array should then generate options in a select/dropdown menu on a form (the function to convert the array into options is already set, it's just a matter of getting the correct array output).

The problem is that when I access the form, all of the rows from the txt file are in one <option> in the select menu. So rather than having a select menu with a few dozen options, I'm getting a select menu with one option that contains a few dozen rows of data from the txt file.

Upvotes: 1

Views: 49

Answers (1)

M. Adam Kendall
M. Adam Kendall

Reputation: 1282

By doing $list = "" and using string append in your while ($list .= ...) you are creating one large string. If you want an array, then create an array, and append to the array.

$file_handle = fopen("/file.txt", "r");
$list = array();
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 0, "\t");
    $list[] = $line_of_text[10] . " - " . $line_of_text[9] . ": " . $line_of_text[6];
}
fclose($file_handle);

Upvotes: 2

Related Questions