开发者

I need to get a count of values (grouped) in a csv column

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","OR10开发者_如何转开发00x6","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"


$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...
);


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜