将数组结果写入 csv 文件时出现问题

发布于 2024-12-07 05:22:40 字数 1346 浏览 0 评论 0原文

我有一个 csv 文件需要清理。它包含 13 个字段,但我只需要 7 个字段(公司、地址、城市、街道、邮政编码、电话、电子邮件),

我需要遍历所有记录并仅创建包含电子邮件地址的记录的新输出。

简而言之...我加载原始文件,运行 for 循环,分解结果,然后查找 $tmp[10] 索引不为空的记录。然后,我获取其余必需字段,并执行 foreach 循环并将结果写入新的 csv 文件。

根据我如何调整代码,我会得到...... 仅包含电子邮件地址的文本文件。 或者 仅包含最后一条记录和电子邮件地址的文本文件。

我已经在这方面工作太久了,我只需要一双新的眼睛来指出问题。我是 php 新手,想要完成这项工作。提前致谢。

    <?php
    // See if file exists and is readable
    $file = 'uploads/AK_Accountants.csv';
    $newfile = basename($file,".csv");
    $newfile = $newfile.Date("Ymd").".csw";
    $fileNew = fopen('uploads/AK_'.Date("Ymd").'.csv','w+');
    // Read the file into an array called $sourcefile
    $sourcefile = file($file);
    // Loop through the array and process each line
    for ($i = 0; $i < count($sourcefile); $i++) {
        // Separate each element and store in a temp array
       $tmp = explode('","', $sourcefile[$i]);
        // Assign each element of the temp array to a named array key
        if ($tmp[10] != "") {
            $sourcefile[$i] = array('Business_Name' => $tmp[1], 'Address' => $tmp[3], 'City' =>  $tmp[4], 'State' => $tmp[5], 'Zip' => $tmp[6], 'Phone' => $tmp[7], 'Email' => $tmp[10]);
            foreach($sourcefile[$i] as $key => $value);
                fwrite($fileNew, $value);
        }
    }
    ?>

I have a csv file I need to cleanup. It contains 13 fields, but I only need 7 (Business, Address, City, St, Zip, Phone, Email)

I need to run through all of the records and create a new output of just the records with email addresses.

In nutshell... I load the original file, run the for loop, explode the results, then look for the records where the $tmp[10] index is not null. I then get the rest of the rest of required fields, and do a foreach loop and fwrite the results to a new csv file.

Depending on how I tweak the code, I get either...
A text file of just email addresses.
or
A text file of just the last record with an email address.

I have been working on this too long and I just need a fresh set of eyes to point out the problem. I am new to php, and want to make this work. Thanks on advance.

    <?php
    // See if file exists and is readable
    $file = 'uploads/AK_Accountants.csv';
    $newfile = basename($file,".csv");
    $newfile = $newfile.Date("Ymd").".csw";
    $fileNew = fopen('uploads/AK_'.Date("Ymd").'.csv','w+');
    // Read the file into an array called $sourcefile
    $sourcefile = file($file);
    // Loop through the array and process each line
    for ($i = 0; $i < count($sourcefile); $i++) {
        // Separate each element and store in a temp array
       $tmp = explode('","', $sourcefile[$i]);
        // Assign each element of the temp array to a named array key
        if ($tmp[10] != "") {
            $sourcefile[$i] = array('Business_Name' => $tmp[1], 'Address' => $tmp[3], 'City' =>  $tmp[4], 'State' => $tmp[5], 'Zip' => $tmp[6], 'Phone' => $tmp[7], 'Email' => $tmp[10]);
            foreach($sourcefile[$i] as $key => $value);
                fwrite($fileNew, $value);
        }
    }
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

韶华倾负 2024-12-14 05:22:40

快速浏览一下:

foreach($sourcefile[$i] as $key => $value);
    fwrite($fileNew, $value);

应该是

foreach($sourcefile[$i] as $key => $value){
    fwrite($fileNew, $value);
}

另外,你所拥有的

$newfile = $newfile.Date("Ymd").".csw";

而不是我认为应该的

$newfile = $newfile.Date("Ymd").".csv";

From a quick glance:

foreach($sourcefile[$i] as $key => $value);
    fwrite($fileNew, $value);

should be

foreach($sourcefile[$i] as $key => $value){
    fwrite($fileNew, $value);
}

Also, you have

$newfile = $newfile.Date("Ymd").".csw";

rather than what I assume should be

$newfile = $newfile.Date("Ymd").".csv";
初懵 2024-12-14 05:22:40

您的最后一个 foreach 语句以 ';' 终止并且没有代码块。一旦 foreach 语句完成迭代,您将获得写入文件的最后一个值,即电子邮件地址。

你目前已经做到了

foreach (... ) ;
fwrite(...);.

,但你可能的意思是

foreach( ... ) {
    fwrite(...) ;
}

去过那里,做过那件事:)

HTH

Your last foreach statement is terminated by a ';' and has no code block. Once the foreach statement has finished iterating you'll get the last value written to file i.e. just the email address.

You currently have

foreach (... ) ;
fwrite(...);.

but you probably mean

foreach( ... ) {
    fwrite(...) ;
}

Been there, done that :)

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文