使用 PHP 搜索 CSV 文件

发布于 2024-10-08 04:21:54 字数 1445 浏览 1 评论 0原文

我有一个 csv 文件,如下所示:

603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2

我正在使用 fgetcsv 导入该文件,然后将其导出到 xml 并使用 html 正确显示。

需要按照Office(2n字段中的ATL)和Vendor来组织。该文件不会以任何方式排序。搜索文件的最佳方式是什么?

这是我到目前为止所拥有的:

if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) { 
      while (($data = fgetcsv($handle, 1000, ","))) {  
         $currOffice = substr($data[2], 0, 3);
                  foreach ($data as $key => $d) {
                    if ($key != 1){
                    $invoiceData = "<div class=\"field\">" .$d . "</div>";
                    }
                    $prevVendor = $data[3];
                    $prevOffice = $currOffice;
                } 
      }
  }

I have a csv file that looks like this:

603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2

I'm using fgetcsv to import this file and then export it to xml and display it properly with html.

It needs to be organized according to Office (ATL in 2n field) and Vendor. The file will not be sorted in any which way. What's the best way to search through the file?

This is what I have so far:

if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) { 
      while (($data = fgetcsv($handle, 1000, ","))) {  
         $currOffice = substr($data[2], 0, 3);
                  foreach ($data as $key => $d) {
                    if ($key != 1){
                    $invoiceData = "<div class=\"field\">" .$d . "</div>";
                    }
                    $prevVendor = $data[3];
                    $prevOffice = $currOffice;
                } 
      }
  }

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

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

发布评论

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

评论(2

百变从容 2024-10-15 04:21:54

首先(这只是我)我会通过创建一个多维数组来清理数据,如下所示...

if (file_exists('ATLANTA.csv') != FALSE) {
    $handle  = file_get_contents('ATLANTA.csv') or exit;
    $handle_row = explode("\n", $handle);
    foreach ($handle_row as $key => $val) {
        $row_array = explode(',', $val);
        foreach ($row_array as $key => $val) {
            $row_array[$key] = trim(str_replace('"', '', $val));
            }
        $complete[] = $row_array;
        }
    }

然后,通过以下函数传递 $complete 以按第二层数组的值进行排序。

$array_elem_01 = 2; // Which Column to Sort By
$array_elem_02 = 3; // <-- Edit
if (isset($complete)) {
    foreach ($complete as $key => $val) {
        $sort_01[$key] = $val[$array_elem_01] . $val[$array_elem_02]; // <-- Edit
        }
    asort($sort_01);
    foreach ($sort_01 as $key => $val) {
        $output[] = $complete[$key];
        }
    }
return $output;

First (and this is just me) I would clean the data up by creating a multi-dimensional array like so...

if (file_exists('ATLANTA.csv') != FALSE) {
    $handle  = file_get_contents('ATLANTA.csv') or exit;
    $handle_row = explode("\n", $handle);
    foreach ($handle_row as $key => $val) {
        $row_array = explode(',', $val);
        foreach ($row_array as $key => $val) {
            $row_array[$key] = trim(str_replace('"', '', $val));
            }
        $complete[] = $row_array;
        }
    }

Then, pass $complete through the following function to sort by your second tier arrays' values.

$array_elem_01 = 2; // Which Column to Sort By
$array_elem_02 = 3; // <-- Edit
if (isset($complete)) {
    foreach ($complete as $key => $val) {
        $sort_01[$key] = $val[$array_elem_01] . $val[$array_elem_02]; // <-- Edit
        }
    asort($sort_01);
    foreach ($sort_01 as $key => $val) {
        $output[] = $complete[$key];
        }
    }
return $output;
站稳脚跟 2024-10-15 04:21:54

查看 array_search() 函数的 PHP 手册页

它并没有完全满足您的需求,但页面上最新的用户评论提供了一个搜索多级数组的功能,在我看来,它非常适合您的要求。

希望有帮助。

Take a look at the PHP manual page for the array_search() function.

It doesn't do exactly what you need, but the most recent user comment on the page provides a function that searches a multi-level array, that looks to me like it would be a perfect match for your requirements.

Hope that helps.

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