RobertAmsterdam
Members-
Posts
28 -
Joined
-
Last visited
Profile Information
-
Gender
Not Telling
-
Location
Ukraine, Kiev
RobertAmsterdam's Achievements
Newbie (1/5)
0
Reputation
-
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.
-
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.
-
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
-
A question: You use callback function 'trim' there? Where is that function?
-
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 ?
-
How to Aggregate Values from CSV file
RobertAmsterdam replied to RobertAmsterdam's topic in PHP Coding Help
$state = $dataRow[5]; $balance = $dataRow[12]; if (!isset($sumArray[$state])) { $sumArray[$state] = 0; } $sumArray[$state] += $balance; // add balance for state was solution -
How to Aggregate Values from CSV file
RobertAmsterdam replied to RobertAmsterdam's topic in PHP Coding Help
I could do like this: $inputfile = 'input.csv'; $inputHandle = fopen($inputfile, "r"); $output = array(); while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE){ unset($data[0],$data[1],$data[2], $data[3], $data[4], $data[6], $data[7], $data[8], $data[9], $data[10], $data[11] ); // remove redundant fields $data = array_values($data); // restore keys } foreach($data as $key => $line) { list($state, $balance) = $line; $state = trim($state); $balance = trim($balance); $output[$state] += (int)$balance; //file_put_contents('summary.txt', var_export($output, TRUE)); } file_put_contents('summary.txt', var_export($output, TRUE)); but i get Invalid argument supplied for foreach() for some reason -
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...
-
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
-
How to Aggregate Values from CSV file
RobertAmsterdam replied to RobertAmsterdam's topic in PHP Coding Help
There is like 12 rows, should I list each and every one of them, when I need only 2? -
Hello, I have a CSV file that looks like this: "Account","State","Zip","Email","Birth Date","Favorites","Balance" with data rows in them. I need to calculate total balances aggregated by state, need to get output in .txt flle like: State, Balance value, value this is how I started : $inputfile = 'input.csv'; $inputHandle = fopen($inputfile, "r"); $output = array(); while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE){ foreach($data as $line) { list($state, $balance) = $data; $state = trim($state); $balance = trim($balance); $output[$state] += (int)$balance; file_put_contents('summary.txt', var_export($output, TRUE)); } } I get unidentified inxex errors on $output[$state] += (int)$count; May be i make some mistake in line: list($state, $balance) = $data; Could someone maybe propose a better, more convinient solution here?
-
ok I saw the error. $headerRow in second part of the loop was not needed. Everything is perfectly formatted! Million Thanks!!! I've learned so much practical stuff with you!
-
ok if this is what you mean: while (($dataRow = fgetcsv($inputHandle, 1000, ",")) !== FALSE) { if($headerRow == FALSE) { $headerRow = $dataRow; continue; //Start next iteration of the loop } print("<pre>".print_r($headerRow, true)."</pre>"); // now the header row is ok, contains all good fields { //Set flag so additional rows will be processed $headerRow = TRUE; //Modify the name $dataRow[$nameColumn] = formatName($dataRow[$nameColumn]); //Modify the date $dataRow[$BDColumn] = formatDate($dataRow[$BDColumn]); //put modified data in array $tempData[] = $dataRow; } //print("<pre>".print_r($headerRow, true)."</pre>"); // but here output is 1... } at the end it has 1, what would overwrite it?
-
You mean like this? $inputfile = 'input.csv'; $outputFile = 'output.csv'; $nameColumn = 1; $BDColumn = 8; function formatName($name) { $nameArray = explode(' ', $name); $lastName = array_pop($nameArray); $name = $lastName . (count($nameArray) ? ', ' : '' ) . implode(' ', $nameArray); return $name;; } function formatDate($date) { $timestamp = strtotime($date); //if there is timestamp in a row, do data bd conversion else it is header field if($timestamp) { $date = date('Ymd', $timestamp); } return $date; } $inputHandle = fopen($inputfile, "r"); $outputHandle = fopen($outputFile, 'w'); //Temporary array to hold processed data $tempData = array(); //Flag to not process the header data $headerRow = FALSE; // - setting to false while (($dataRow = fgetcsv($inputHandle, 1000, ",")) !== FALSE) { //Do not process the first row of data if($headerRow == FALSE) { //Set flag so additional rows will be processed $headerRow = $dataRow; // - getting the first raw $dataRow[$nameColumn] = formatName($dataRow[$nameColumn]); $dataRow[$BDColumn] = formatDate($dataRow[$BDColumn]); $tempData[] = $dataRow; } } usort($tempData, 'sortMyArray'); sortMyArray($tempData[$nameColumn], SORT_REGULAR); function sortMyArray($a, $b) { $nameColumn = 1; return strcmp($a[$nameColumn], $b[$nameColumn]); } fputcsv($outputHandle, $headerRow); // header first foreach ($tempData as $dataRow) { fputcsv($outputHandle, $dataRow); } fclose($inputHandle); fclose($outputHandle); I get Undefined offset: 1 on sortMyArray($tempData[$nameColumn], SORT_REGULAR); line What does that mean?
-
what actually happend is that sort(), moved this header row somewhere to the middle of the CSV file.