RobertAmsterdam Posted July 3, 2013 Share Posted July 3, 2013 (edited) 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 July 3, 2013 by RobertAmsterdam Quote Link to comment Share on other sites More sharing options...
cpd Posted July 3, 2013 Share Posted July 3, 2013 I'm a little confused as to how your data is represented in the CSV file, could you elaborate slightly? Quote Link to comment Share on other sites More sharing options...
RobertAmsterdam Posted July 3, 2013 Author Share Posted July 3, 2013 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... Quote Link to comment Share on other sites More sharing options...
cpd Posted July 3, 2013 Share Posted July 3, 2013 (edited) 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 July 3, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2013 Share Posted July 3, 2013 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); Quote Link to comment Share on other sites More sharing options...
RobertAmsterdam Posted July 3, 2013 Author Share Posted July 3, 2013 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 ? Quote Link to comment Share on other sites More sharing options...
RobertAmsterdam Posted July 3, 2013 Author Share Posted July 3, 2013 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2013 Share Posted July 3, 2013 trim Quote Link to comment Share on other sites More sharing options...
RobertAmsterdam Posted July 3, 2013 Author Share Posted July 3, 2013 (edited) trim 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 July 3, 2013 by RobertAmsterdam Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2013 Share Posted July 3, 2013 What does data.csv look like? Quote Link to comment Share on other sites More sharing options...
RobertAmsterdam Posted July 3, 2013 Author Share Posted July 3, 2013 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 4, 2013 Share Posted July 4, 2013 Was it too difficult to paste a couple of sample lines, or to punctuate that reply to show birth and date are one field (I assume anyway)? If you can't be bothered to give better information, why should I? Quote Link to comment Share on other sites More sharing options...
Solution RobertAmsterdam Posted July 4, 2013 Author Solution Share Posted July 4, 2013 (edited) 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 July 4, 2013 by RobertAmsterdam Quote Link to comment Share on other sites More sharing options...
Barand Posted July 4, 2013 Share Posted July 4, 2013 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.