使用PHP将数据添加到特定列的CSV文件中 [英] Add data to csv file in specific columns using PHP

查看:299
本文介绍了使用PHP将数据添加到特定列的CSV文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,其中包含14列和数百行。标题为 sku,类别,描述,品牌 等。

I have a csv file which has 14 columns, and hundreds of rows. There is a header which is "sku","category","description","brand" etc.

所有数据都已准备就绪,但我正在尝试在CSV中的某些特定列中添加一些图像文件名( images, small_image, thumnail)。 >

All data is allready there but I'm trying to add some image filenames inside the CSV, in some specific columns ( "images", "small_image" , "thumnail" ).

标题看起来像这样:

+-----+----------+-------------+-------+-------------+-----------+--------+------+
| sku | category | description | image | small_image | thumbnail |  price | color|
+-----+----------+-------------+-------+-------------+-----------+--------+------+

所以知道标题的样子后,第一行就是:

so knowing how the header looks like, then the first row would be like:

+-------+---------------+------------------+---+---+----+-----+--------+
| sku01 | category name | description here |   |   |    | 99$ | black  |
+-------+---------------+------------------+---+---+----+-----+--------+

jpeg的文件名与同一行中的 sku 值相同,并带有jpeg扩展名。因此,例如,如果第一行的 sku 列有 sku01 ,则文件名将为 sku01.jpeg 在同一行的图片列中。

The filename for the jpeg is the same as the sku value on the same row , with the jpeg extension. So for example, if there is sku01 for the sku column on the first row, the filename would be sku01.jpeg in the image column on that same row. So would be the small_image value, and the thumbnail value.

但是,只有在文件夹中存在jpeg的情况下,才指定文件名。

到目前为止,我知道如何使用fopen函数打开csv文件,最终将所有数据存储在数组中(我不知道这是否对我有帮助),然后在检查服务器上特定文件夹中的file_exist是否存在后,使用fputcsv函数。

So far I know how to use the fopen function to open the csv file, eventually store all data in an array ( I don't know if that would help in my case ) , then at some point use the fputcsv function after checking if file_exist in the specific folder on the server.

但是,就我应该使用这些功能的顺序而言,对我而言,这仍然是一个很大的麻烦。我被卡住了。另外,我不知道如何将jpeg文件名放在正确的列(图像,缩略图,small_image)中,因为这些列位于csv文件的中间以及其他列中。它们不在csv文件的末尾

But, to me it's still a big mess in my head as far as in what order I should use the functions. I 'm stuck. Also, I have no idea how to put the jpeg filename in the right columns ( image, thumbnail, small_image), because these columns are in the "middle" of the csv file, among other columns. They are not at the end of the csv file

我真的很感谢任何能将我带到正确路径的人。

I would be really thankful to anyone who can put me on the right path.

推荐答案

要执行您想做的事情,首先应将各列完全按原始csv文件中的显示形式分成一个数组:

To do what you want to do, you should first break out your columns into an array exactly as they appear in your raw csv file:

$columns = ["sku","category","description","image","small_image","thumbnail", "price","color"];

然后,您需要打开文件并对其进行迭代,以建立键的关联数组-值对,检查该图像名称是否存在文件,如果存在,则为该数组分配正确的名称。

Then you'll want to open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array.

$rootDir = ""; //Root directory where your image files are located
$file = fopen("filehandle.csv", "r"); //Open the old file for reading
$newFile = fopen("newfilehandle.csv", "w"); //Create a new file for writing

while (($data = fgetcsv($file)) !== FALSE) {
    $row = array_combine($columns, $data);
    $filename = "{$row['sku']}.jpeg";
    if (file_exists("$rootDir/$filename")) {
        $row['image'] = $filename;
        $row['small_image'] = $filename;
        $row['thumbnail'] =  $filename; 
    }
    fputcsv($newFile, array_values($row)); //write data into new file
}

fclose($file);
fclose($newFile);

现在您已经拥有原始文件的副本,并插入了新值。

Now you have a copy of the original file with the new values inserted.

这篇关于使用PHP将数据添加到特定列的CSV文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆