将 csv 文件数据插入数组 (PHP)

发布于 2024-11-04 20:17:57 字数 1200 浏览 0 评论 0原文

我正在尝试将上传文件中的数据插入到一维数组中。

该文件是这样的,一行中有一个学生编号,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抚笙 2024-11-11 20:17:57

问题 1 是因为

print_r($_FILES['csv_file']);
$file = fopen('$file_path', 'r');

应该是:

$file = fopen($file_path, 'r');

对于问题 2,请查看 array_push

Question 1 is because of

print_r($_FILES['csv_file']);
$file = fopen('$file_path', 'r');

should be:

$file = fopen($file_path, 'r');

and for Question 2, check out the array_push

一场信仰旅途 2024-11-11 20:17:57

第一个问题:

这一行实际上会尝试打开一个名为“$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

彻夜缠绵 2024-11-11 20:17:57

根据 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 ",")

甲如呢乙后呢 2024-11-11 20:17:57

以下是对您的代码的一些一般性注释:

  1. 您错误地将文件路径传递到 fopen() 函数中。该变量不应用单引号引起来。
  2. 由于您在处理后删除 CSV 文件,因此无需移动它。只需使用 $_FILES['csv_file']['tmp_name'] 作为文件的路径。
  3. 由于 CSV 文件中每行只有一个条目,因此只需访问从 fgetcsv() 返回的数组的第一个元素即可:$numbers[] = $line[0];

Here are some general comments on your code:

  1. You are passing the file path into the fopen() function incorrectly. The variable should not be surrounded with single quotes.
  2. Since you are deleting the CSV file after processing it, moving it is unnecessary. Simply use $_FILES['csv_file']['tmp_name'] as the path to the file.
  3. Since there is only one entry per row in your CSV file, simply access the first element of the array that is returned from fgetcsv(): $numbers[] = $line[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文