在 PHP 中处理 CSV 文件中的数据
Manipulating data from a CVS file in PHP
好吧,我刚才有点卡住了。
作为我的任务,我必须根据来自 cvs 文件的数据制作一个 "star processor" 这是我的 cvs 文件
Number,Character,Order
5,*,0
4,*,1
3,*,2
2,*,3
1,*,4
2,*,5
3,*,6
4,*,7
5,-,8
我正在尝试输出什么 -
*****
****
***
**
*
**
***
****
-----
希望你明白了
这是我刚才得到的 -
$starsFile = fopen("stars.csv", "r");
while (!feof($starsFile)) {
print_r (fgetcsv($starsFile, ","));
}
fclose($starsFile);
这基本上只是 returns 数组中的 CSV 数据,这很酷!
我遇到的问题是弄清楚如何访问 'fgetcsv' 创建的数组?我可以使用另一种数据结构吗?并提供帮助或指点,我们将不胜感激!
试一试:
$starsFile = fopen("stars.csv", "r");
while (!feof($starsFile)) {
$stars = fgetcsv($starsFile, ",");
print str_repeat($stars[1], intval($stars[0]))."\n";
}
fclose($starsFile);
if (($starsFile = fopen("stars.csv", "r")) !== FALSE) {
while (($data = fgetcsv($starsFile, 1000, ",")) !== FALSE) {
// no. of times the symbol needs to be displayed
$num_symbol_display = (int)$data[0];
// the symbol to be displayed
$symbol = $data[1];
// logic to display the symbol
for ($i=1; $i<=$num_symbol_display; $i++){
echo $symbol;
}
echo "<br>";
}
fclose($starsFile);
}
从最初的问题来看,不清楚第三个字段应该做什么。
好吧,我刚才有点卡住了。
作为我的任务,我必须根据来自 cvs 文件的数据制作一个 "star processor" 这是我的 cvs 文件
Number,Character,Order
5,*,0
4,*,1
3,*,2
2,*,3
1,*,4
2,*,5
3,*,6
4,*,7
5,-,8
我正在尝试输出什么 -
*****
****
***
**
*
**
***
****
-----
希望你明白了
这是我刚才得到的 -
$starsFile = fopen("stars.csv", "r");
while (!feof($starsFile)) {
print_r (fgetcsv($starsFile, ","));
}
fclose($starsFile);
这基本上只是 returns 数组中的 CSV 数据,这很酷!
我遇到的问题是弄清楚如何访问 'fgetcsv' 创建的数组?我可以使用另一种数据结构吗?并提供帮助或指点,我们将不胜感激!
试一试:
$starsFile = fopen("stars.csv", "r");
while (!feof($starsFile)) {
$stars = fgetcsv($starsFile, ",");
print str_repeat($stars[1], intval($stars[0]))."\n";
}
fclose($starsFile);
if (($starsFile = fopen("stars.csv", "r")) !== FALSE) {
while (($data = fgetcsv($starsFile, 1000, ",")) !== FALSE) {
// no. of times the symbol needs to be displayed
$num_symbol_display = (int)$data[0];
// the symbol to be displayed
$symbol = $data[1];
// logic to display the symbol
for ($i=1; $i<=$num_symbol_display; $i++){
echo $symbol;
}
echo "<br>";
}
fclose($starsFile);
}
从最初的问题来看,不清楚第三个字段应该做什么。