Ошибка печати массива fgetcsv - PullRequest
0 голосов
/ 14 мая 2019

!!Пересмотрено !!Почему-то печатается только одна строка csv, а двойная печать страны?csv preview output

<?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);
?>

Ответы [ 2 ]

2 голосов
/ 14 мая 2019

EDIT:

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>
0 голосов
/ 14 мая 2019

Я думаю, у вас есть синтаксическая ошибка в этой строке:

  echo "<a class="item">" . $loc[$i] .",". $state[i] .",". $country[i] . "<a/>";

Для $state[i] и $country[i] они должны быть $state[$i] и $country[$i] соответственно.

...