我正在加载一个CSV文件并循环遍历每一行。我想为每个列分配变量(显示在列表行中),然后在第一个实例中显示每行$project
数据。所以基本上每行都会给我每个项目代码。
我将使用其他变量,但现在只想要这个变量。
应该有1000个项目,但它只显示一个项目,我哪里出错?
<?php
$row = 1;
if (($handle = fopen("users.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, ",")) !== FALSE) {
$num = count($data);
$row++;
list($project, $rod, $rom, $asm, $contract, $site, $town, $postcode, $country) = $data;
echo $contract;
}
fclose($handle);
}
?>
答案 0 :(得分:1)
同时,只需使用
$project = $data[0];
答案 1 :(得分:0)
每次在fgetcsv
语句中调用while
时,都会从csv文件中读取下一行。所以你可以使用......
<?php
if (($handle = fopen("users.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
list($project, $rod, $rom, $asm, $contract, $site, $town, $postcode, $country) = $data;
echo $project.PHP_EOL;
}
fclose($handle);
}
?>
或者获取项目列表......
<?php
if (($handle = fopen("users.csv", "r")) !== FALSE) {
$projects = [];
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
list($project, $rod, $rom, $asm, $contract, $site, $town, $postcode, $country) = $data;
$projects[] = $project;
}
fclose($handle);
print_r($projects);
}
?>