将 csv 文件数据插入数组 (PHP)
我正在尝试将上传文件中的数据插入到一维数组中。
该文件是这样的,一行中有一个学生编号,如下所示:
392232,
392231,
等
这是我在网上找到的最常见的方式:
while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
但是,据我了解,这将创建一个数组($line
) 每行。这不是我想要的。
除此之外,我尝试看看它是否有效,并且我的代码在使用 ftgetcsv() 后没有打印出数组。文件正在上传成功。
这是我的代码:
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){
//create file name
$file_path = "csv_files/" . $_FILES['csv_file']['name'];
//move uploaded file to upload dir
if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
//error moving upload file
echo "Error moving uploaded file";
}
print_r($_FILES['csv_file']);
$file = fopen('$file_path', 'r');
while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
//delete csv file
unlink($file_path);
}
首先,任何人都可以清楚地看出为什么至少将它们打印为单独的数据数组(每行)不起作用。
其次,是否可以对其进行设置,以便创建文件中所有行的一维数组?
非常感谢,
I am attempting to insert the data from an uploaded file into a single dimension array.
The file is as such that there is one student number to a line like so:
392232,
392231,
etc
this is the most common way I've found online:
while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
However form what I understand this will create an array ($line
) for each row. Which is not what I want.
that aside I tried this to see if it is working and my code is not printing out the array after using ftgetcsv(). The file is successfully uploading.
here is my code:
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){
//create file name
$file_path = "csv_files/" . $_FILES['csv_file']['name'];
//move uploaded file to upload dir
if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
//error moving upload file
echo "Error moving uploaded file";
}
print_r($_FILES['csv_file']);
$file = fopen('$file_path', 'r');
while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
//delete csv file
unlink($file_path);
}
First off, can anyone obviously see why it wouldnt work to at least print them as seperate arrays of data (each row).
Second, is it possible to set it so that it creates a 1d array of all rows in the file?
Many thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题 1 是因为
应该是:
对于问题 2,请查看 array_push
Question 1 is because of
should be:
and for Question 2, check out the array_push
第一个问题:
这一行实际上会尝试打开一个名为“$file_path”的文件,因为您使用的是单引号(因此它不会扩展到变量的值)。您只需删除引号即可。
$file = fopen('$file_path', 'r');
$file
此后为空。第二个问题:
如果您只想按行将文件转换为数组,您可以使用其中之一:
file() - 将整个文件放入一维行数组中文件(最接近您想要的)
fgets() - 每次调用每行获取一个字符串;继续调用它直到它返回 false 一次获取每一行
file_get_contents() - 将整个文件放入字符串并进行处理随你便
1st Question:
This line will actually try to open a file called '$file_path' because you're using single quotes (so it doesn't expand to the value of the variable). You can just remove the quotes.
$file = fopen('$file_path', 'r');
$file
is null after this.2nd Question:
If all you want to do is convert a file into an array by lines you can use one of these instead:
file() - get whole file into a 1D array of lines of the file (closest to what you want)
fgets() - get a string per line per call; keep calling this until it returns false to get each line one at a time
file_get_contents() - get the whole file into a string and process as you like
根据 PHP.net $line 必须以数组形式返回。
“返回一个包含读取字段的数组。”
但是,如果您确定它只包含一个学生编号,您可以使用 $line[0] 获取第一行值(忽略“,”)
According to PHP.net $line has to return as array.
"returns an array containing the fields read."
But if you are sure it's contains only one student number you can use $line[0] to get the first line value (Ignoring the ",")
以下是对您的代码的一些一般性注释:
fopen()
函数中。该变量不应用单引号引起来。$_FILES['csv_file']['tmp_name']
作为文件的路径。fgetcsv()
返回的数组的第一个元素即可:$numbers[] = $line[0];
Here are some general comments on your code:
fopen()
function incorrectly. The variable should not be surrounded with single quotes.$_FILES['csv_file']['tmp_name']
as the path to the file.fgetcsv()
:$numbers[] = $line[0];