Jump to content

Cannot count values in Array!


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

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/279825-cannot-count-values-in-array/
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...

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.

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 ?

 

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?

 

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

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.

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,[email protected],2/28/71,XA|MA,101,100,15.89
01-000667,George P Schell,Market Place Products,334 Hilltop Dr,Mentor,OH,44060-1930,[email protected],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,[email protected],6/30/75,BA|MA|RA,144,144,449.92

Not too readable.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.