我需要获取 csv 列中的值(分组)计数

发布于 2024-10-10 10:00:58 字数 1754 浏览 3 评论 0原文

我需要计算第一列的值。这些 ID 可能存在于我收到的任何给定 .csv 文件中,也可能不存在。因此,我需要循环遍历 .csv 文件,查看第一列,如果不存在,则将其添加到保持数组 ($PWSs) 中,或者如果我已经添加了该保持数组,则增加该保持数组中的计数。

我使用 fgetcsv() 进行了第一个循环。这适用于破解文件:

$PWSs = array();

$handle2 = fopen ($uploadfileandpath,"r");
while ($field2array = fgetcsv ($handle2, 130000, ",")) 
{
    // Here is where I would add value or increment $PWSs array
    while (?)
    {
        if ($field2array[0] != ?)
        {
            // Add or increment
        }
    }
}

这是实际数据。第一列包含公共供水系统的 ID。我需要数一下它们。

"00513","08/13/2009","090834311A","R","4","OR1000x6","N","N","E",,1,".73","COLILERT"
"00513","08/13/2009","090834312A","R","39","OR1000x6","N","N","E",,1,".35","COLILERT"
"00154","08/13/2009","090835401A","R","300 Falls Road","OR100016","N","N","E",,1,".10","COLILERT"
"95343","08/13/2009","090835601A","R","Room 1 Sink","OR1000x6","N","N","E",,1,,"COLILERT"
"94585","08/14/2009","090837701A","R","Kitchen","OR1000x6","N","N","E",,1,,"COLILERT"
"94704","08/14/2009","090837801A","R","Outside Tap","OR1000x6","N","N","E",,1,,"COLILERT"
"01430","08/14/2009","090838201A","R","100 Deer Park Ln OT","OR1000x6","N","N","E",,1,,"COLILERT"
"00625","08/14/2009","090839001A","R","Dano and N Rose","OR100016","N","N","E",,1,".35","COLILERT"
"00405","08/17/2009","090840301A","R","Westmont Drive","OR100016","N","N","E",,1,".28","COLILERT"
"01031","08/17/2009","090840401A","R","Unit 2 Faucet","OR100016","N","N","E",,1,,"COLILERT"
"00625","08/17/2009","090840601A","R","Luman Road","OR1000x6","N","N","E",,1,".35","COLILERT"
"00513","08/17/2009","090841001A","R","40","OR1000x6","N","N","E",,1,".18","COLILERT"
"00513","08/17/2009","090841002A","R","10","OR1000x6","N","N","E",,1,".16","COLILERT"

I need to get a count of the first column's values. These ID's might or might not exist in any given .csv file I receive. So I need to loop through the .csv file looking at the first column and either adding it to a holding array ($PWSs) if it doesn't exist or incrementing the count in this holding array if I've already added it.

I have the first loop using fgetcsv()..this works for cracking into the file:

$PWSs = array();

$handle2 = fopen ($uploadfileandpath,"r");
while ($field2array = fgetcsv ($handle2, 130000, ",")) 
{
    // Here is where I would add value or increment $PWSs array
    while (?)
    {
        if ($field2array[0] != ?)
        {
            // Add or increment
        }
    }
}

Here is actual data. The first column has IDs for Public Water Systems. I need to count them.

"00513","08/13/2009","090834311A","R","4","OR1000x6","N","N","E",,1,".73","COLILERT"
"00513","08/13/2009","090834312A","R","39","OR1000x6","N","N","E",,1,".35","COLILERT"
"00154","08/13/2009","090835401A","R","300 Falls Road","OR100016","N","N","E",,1,".10","COLILERT"
"95343","08/13/2009","090835601A","R","Room 1 Sink","OR1000x6","N","N","E",,1,,"COLILERT"
"94585","08/14/2009","090837701A","R","Kitchen","OR1000x6","N","N","E",,1,,"COLILERT"
"94704","08/14/2009","090837801A","R","Outside Tap","OR1000x6","N","N","E",,1,,"COLILERT"
"01430","08/14/2009","090838201A","R","100 Deer Park Ln OT","OR1000x6","N","N","E",,1,,"COLILERT"
"00625","08/14/2009","090839001A","R","Dano and N Rose","OR100016","N","N","E",,1,".35","COLILERT"
"00405","08/17/2009","090840301A","R","Westmont Drive","OR100016","N","N","E",,1,".28","COLILERT"
"01031","08/17/2009","090840401A","R","Unit 2 Faucet","OR100016","N","N","E",,1,,"COLILERT"
"00625","08/17/2009","090840601A","R","Luman Road","OR1000x6","N","N","E",,1,".35","COLILERT"
"00513","08/17/2009","090841001A","R","40","OR1000x6","N","N","E",,1,".18","COLILERT"
"00513","08/17/2009","090841002A","R","10","OR1000x6","N","N","E",,1,".16","COLILERT"

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

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

发布评论

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

评论(3

情深缘浅 2024-10-17 10:00:58
$fh = fopen('file.csv', 'rb');

$PWS = array();
while($row = fgetcsv($fh)) {
    $PWS[$row[0]]++;
}

基本上,它将使用第一列值作为键来填充 PWS,并在它们出现时递增它们。然后,根据上面的 csv 示例,您最终会得到

$PWS = array(
    '00513' => 4
    '00154' => 1
    '95343' => 1
    '94585' => 1
etc...
);
$fh = fopen('file.csv', 'rb');

$PWS = array();
while($row = fgetcsv($fh)) {
    $PWS[$row[0]]++;
}

Basically it'll populate the PWS using the first column values as keys, and increment them as they come along. Afterwards, given your sample csv above, you'd end up with

$PWS = array(
    '00513' => 4
    '00154' => 1
    '95343' => 1
    '94585' => 1
etc...
);
不语却知心 2024-10-17 10:00:58
function get_pws()
{
    $PWSs = array();

    $handle2 = fopen ($uploadfileandpath,"r");
    while ($field2array = fgetcsv ($handle2, 130000, ",")) 
    {
        if(!in_array($field2array[0], $PWSs))
        {
            array_push($PWSs, array('key'=>$field2array[0], 'count'=>1));
        }
        else
        {
            foreach($PWSs as &$PWS)
            {
                if($PWS['key'] == $field2array[0])
                {
                    ++$PWS['count'];
                }
            }
        }
    }

    return $PWSs;
}

我实际上还没有运行和测试这个脚本,所以希望它能工作并且这就是您正在寻找的;)

编辑:感谢您指出 dq.再说一遍,我还没有测试过它(不是在安装了 PHP atm 的机器上),所以希望它仍然有效(如果它首先有效):P

function get_pws()
{
    $PWSs = array();

    $handle2 = fopen ($uploadfileandpath,"r");
    while ($field2array = fgetcsv ($handle2, 130000, ",")) 
    {
        if(!in_array($field2array[0], $PWSs))
        {
            array_push($PWSs, array('key'=>$field2array[0], 'count'=>1));
        }
        else
        {
            foreach($PWSs as &$PWS)
            {
                if($PWS['key'] == $field2array[0])
                {
                    ++$PWS['count'];
                }
            }
        }
    }

    return $PWSs;
}

I haven't actually run and tested this script, so hopefully it works and it's what you're looking for ;)

Edit: Thanks for pointing that out dq. Again, I haven't tested it (not on a machine with PHP installed atm), so hopefully it still works (if it worked in the first place) :P

殤城〤 2024-10-17 10:00:58

您只需要一个 while 循环。当到达 eof 时,外部 while 循环将停止,因为 fgetcsv() 将返回 FALSE。

然后只需测试该列是否为 NULL 或“”为空字符串。如果给定数组中不存在该列,则应使用 isset() 确保它首先存在于条件中。

You only need one while loop. Your outer while loop will stop when it hits the eof, because fgetcsv() will return FALSE.

Then just test for the column to be NULL or "" an empty string. If the column did not exist in the given array, you should use isset() to make sure it exists first in your conditional.

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