I am importing a csv that could potentially have around 100,000 rows. Each row has 5 columns. The first column of each row will have a sentence and the other 4 columns have numeric values. I need to loop through the csv data and grab each word from each row and add it as a it's own row in a new array. So for example:
| big red truck | 5 | 2 | 5 | 1 |
| small red truck | 4 | 2 | 0 | 0 |
| big fast truck | 3 | 2 | 4 | 1 |
becomes
| truck | 12 | 6 | 9 | 2 |
| red | 9 | 4 | 5 | 1 |
| fast | 3 | 2 | 4 | 1 |
| small | 4 | 2 | 0 | 0 |
This is what I'm currently doing. It works fine with smaller files but at around 50,000 rows I run into issues and start getting back server errors.
function get_csv_terms($csvdata){
$terms = array();
$csv_rows = count($csvdata);
$x = 0;
//get terms
while($x <= $csv_rows){
$groupTerm = explode(' ', $csvdata[$x][0]);
foreach( $groupTerm as $term ){
if($term != NULL){
if(!in_array($term, $terms)){
$terms[] = $term;
}
}
}
$x++;
}
return $terms;
}
//filter csv and create data for table output
function filter_csv($csvdata){
$sortedData = array();
$csv_rows = count($csvdata);
$terms = get_csv_terms($csvdata);
$terms_count = count($terms);
$x = 0;
while($x <= $terms_count){
$y = 0;
while($y <= $csv_rows){
$termWords = explode(" ", $csvdata[$y][0]);
$termWordCount = count($termWords);
$z = 0;
while($z <= $termWordCount){
if($terms[$x] != NULL){
if($termWords[$z] == $terms[$x]){
$sortedData[$terms[$x]][0] += intval($csvdata[$y][1]);
$sortedData[$terms[$x]][1] += floatval($csvdata[$y][2]);
$sortedData[$terms[$x]][2] += floatval($csvdata[$y][3]);value
$sortedData[$terms[$x]][3] += floatval($csvdata[$y][4]);
}
}
$z++;
}
$y++;
}
$x++;
}
return $sortedData;
}