Jump to content

Cannot count values in Array!


Go to solution Solved by RobertAmsterdam,

Recommended Posts

Hi!

 

I need to count number of occurances of each value in array.

 

Input file(this is one row):

 

Teams
Red
Green | Red | Yellow
Red
Yellow | Green | Yellow
Red
Green
Yellow | Yellow | Red
Red
Green
Red
Yellow
 
Need to count top 5 most occurring teams. And need to count number of times each of those top 5 values appear.
 
My pathetic attempt:
$inputfile = 'count.csv';
        $inputHandle = fopen($inputfile, "r");
        
        while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE) {
        
               $teams = $data[0];

               $teams = explode('|', $teams);
        
               $count[] = (array_count_values($teams));
      } print_r($count);

Only prints out the multidimensional array , since I had to use explode with delimiter. But values are not counted.

 

Could someone point me in the right direction?

 

Thanks

 

 

 

 

 

Edited by RobertAmsterdam
Link to comment
Share on other sites

Sure,

 

This is one field with many rows. Each row has name/names of the team(s). Delimited with ' | '.

 

The challenge is to count top 5 . And number of times they appear.  Since there may be multiple teams in a cell, it makes it difficult to use array_count_values()

 

Being a noob, and not very good at arrays, I' quite lost here...

Link to comment
Share on other sites

Assuming the names are consistent throughout the CSV, i.e. they all start with Upper-case and are unique, you can cycle through them all in a similar way you already have and create a new array, then natural order the array.

$teamTotal = array();
while(($d = fgetcsv($fh, 1024) !== false) {
   $teams = explode("|", $d[0]);
   foreach($teams as $t) {
      if(isset($teamTotal[$t])) {
         $teamTotal[$t]++;
      } else {
         $teamTotal[$t] = 1;
      }
   }
}
 
arsort($teamTotal);

You can now cycle through them in order highest-lowest. This algorithm however, will take a while to complete with large data so you may want to consider a different data structure other than an array if speed is important/you have large data.

Edited by cpd
Link to comment
Share on other sites

If I run it, like this:

$inputfile = 'count.csv';
        $inputHandle = fopen($inputfile, "r");
               
        while(($data = fgetcsv($inputHandle, 1024)) !== false) {
       
            $teams = explode("|", $data[0]);
               
            foreach($teams as $t) {
                  
                if(isset($teamTotal[$t])) {
                     $teamTotal[$t]++;
                  } else {
                     $teamTotal[$t] = 1;
                  }
                }
                arsort($teamTotal);             
  }
  print("<pre>".print_r($teamTotal, true)."</pre>");   

I get:

Array
(
['Red'] => 5
[ Yellow'] => 2
['Yellow ] => 2
['Green'] => 2
[ Red'] => 1
['Yellow'] => 1
[ Green ] => 1
['Green ] => 1
[ Red ] => 1
[Teams] => 1
[ Yellow ] => 1
)

output. Is it possible to know total of Red's. Green's and Yellow's ?

Link to comment
Share on other sites

 

try

$data = file('test.txt');
$teams = array();

foreach ($data as $line) {
    $teams = array_merge($teams, explode('|', $line));
}
$teams = array_map('trim', $teams);
$counts = array_count_values($teams);
arsort($counts);

 

A question: You use callback function 'trim' there?

Where is that function?

Link to comment
Share on other sites

 

This works as it is. When I try to modify it however with another input file:

$outputFile = 'test.'.txt';
$outputHandle = fopen($outputFile, 'w');   
$inputfile = 'data.csv';
$inputHandle = fopen($inputfile, "r");

$favorites = array();
while(($data = fgetcsv($inputHandle, 1024) !== false)) {
       $favorites[] = $data[9];

}
foreach ($data as $line) {
    $favorites = array_merge($favorites, explode('|', $line));
   }
$favorites = array_map('trim', $favorites);
$counts = array_count_values($favorites);
arsort($counts);

I get: Invalid argument supplied for foreach()

 

I obviously broke it somewhere, but $favorites is an array

Edited by RobertAmsterdam
Link to comment
Share on other sites

What does data.csv look like?

 

 

 

Account Number Name Company Street City State Zip Email Birth Date Favorites Standard Payment Latest Payment Balance

I am getting values from Favorites column, which contains ' | ' delimited fields, like those in example above.

Link to comment
Share on other sites

  • Solution

sorry file a bit too large, here is some part: 

Account Number,Name,Company,Street,City,State,Zip,Email,Birth Date,Favorites,Standard Payment,Latest Payment,Balance
01-000667,George P Schell,Market Place Products,334 Hilltop Dr,Mentor,OH,44060-1930,warmst864@aol.com,2/28/71,XA|MA,101,100,15.89
01-000667,George P Schell,Market Place Products,334 Hilltop Dr,Mentor,OH,44060-1930,warmst864@aol.com,2/28/71,XA|MA,101,100,15.89
01-002423,Marc S Brittan,Madson & Huth Communication Co,"5653 S Blackstone Avenue, #3E",Chicago,IL,60637-4596,mapper@tcent.net,6/30/75,BA|MA|RA,144,144,449.92

Not too readable.

Edited by RobertAmsterdam
Link to comment
Share on other sites

try

$favourites = array();

$fh = fopen('test.txt', 'r');

$line = fgetcsv($fh, 1024); // header
while ($line = fgetcsv($fh, 1024)) {
    $data = $line[9];
    $favourites = array_merge($favourites, explode('|', $data));
}
$favourites = array_map('trim', $favourites);
$counts = array_count_values($favourites);
arsort($counts);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.