I am having problems trying to figure out how to to read in a tab delimited text file such that I can access the contents as an associative array.
For example, I have a tab delimited file (listing.txt) that contains items as follows (note this example has spaces, but the real file indeed uses tabs):
100 $10 L Red
200 $20 M Green
300 $30 S Blue
My PHP code is as follows:
<?
$list = file("listing.txt");
for ($i = 0; $i<count($list); $i++) {
$line = explode("\t", $list[$i]);
$item[$line[0]][id] = $line[0];
$item[$line[0]][price] = $line[1];
$item[$line[0]][size] = $line[2];
$item[$line[0]][color] = $line[3];
}
echo $item[200][color]; // Should be "Green"
?>
I would think that the above would retrieve the requested data and print out "Green". However, it returns nothing. Where have I gone wrong?
Any suggestions would be most appreciated.
-Bob