fgetcsv 数组打印失败
fgetcsv array print failing
!!修改 !!
出于某种原因,它只打印一行 csv,并双重打印国家?
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
list($country[], $state[], $city[], $loc[]) = $line;
$arrayLength = count($line);
$i = 0;
while ($i < $arrayLength) {
echo "<a class=\"item\">" . $loc[$i] .",". $city[$i] .",". $state[$i] .",".$country[$i] . "<a/></br>";
$i++;
}
}
fclose($file);
?>
编辑:
CSV 文件
United States,Alabama,Alburn,Earth Fare
United States,Alabama,Huntsville,Earth Fare
United States,Alabama,Montgomery,Earth Fare
United States,Alabama,Hoover,Earth Fare
United States,Alabama,Fairhope,Fairhope Health Foods
代码:
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
// Extract line array into these variables
list($country, $state, $city, $loc) = $line;
echo "<a class=\"item\">$loc, $city, $state, $country</a>\n";
}
fclose($file);
?>
结果:
<a class="item">Earth Fare, Alburn, Alabama, United States</a>
<a class="item">Earth Fare, Huntsville, Alabama, United States</a>
<a class="item">Earth Fare, Montgomery, Alabama, United States</a>
<a class="item">Earth Fare, Hoover, Alabama, United States</a>
<a class="item">Fairhope Health Foods, Fairhope, Alabama, United States</a>
我认为你在这一行有语法错误:
echo "<a class="item">" . $loc[$i] .",". $state[i] .",". $country[i] . "<a/>";
对于$state[i]
和$country[i]
,它们应该分别是$state[$i]
和$country[$i]
。
!!修改 !!
出于某种原因,它只打印一行 csv,并双重打印国家?
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
list($country[], $state[], $city[], $loc[]) = $line;
$arrayLength = count($line);
$i = 0;
while ($i < $arrayLength) {
echo "<a class=\"item\">" . $loc[$i] .",". $city[$i] .",". $state[$i] .",".$country[$i] . "<a/></br>";
$i++;
}
}
fclose($file);
?>
编辑:
CSV 文件
United States,Alabama,Alburn,Earth Fare
United States,Alabama,Huntsville,Earth Fare
United States,Alabama,Montgomery,Earth Fare
United States,Alabama,Hoover,Earth Fare
United States,Alabama,Fairhope,Fairhope Health Foods
代码:
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
// Extract line array into these variables
list($country, $state, $city, $loc) = $line;
echo "<a class=\"item\">$loc, $city, $state, $country</a>\n";
}
fclose($file);
?>
结果:
<a class="item">Earth Fare, Alburn, Alabama, United States</a>
<a class="item">Earth Fare, Huntsville, Alabama, United States</a>
<a class="item">Earth Fare, Montgomery, Alabama, United States</a>
<a class="item">Earth Fare, Hoover, Alabama, United States</a>
<a class="item">Fairhope Health Foods, Fairhope, Alabama, United States</a>
我认为你在这一行有语法错误:
echo "<a class="item">" . $loc[$i] .",". $state[i] .",". $country[i] . "<a/>";
对于$state[i]
和$country[i]
,它们应该分别是$state[$i]
和$country[$i]
。