I know this may seem a bit basic, but I'm having real issues with what seems like a simple task.
I have about 60 linear arrays from which I need to produce percentages. I need to "walk" through each array and divide each value by the count of the array (which I have set as a variable) and then multiply by 100 and round to 1 decimal place.
I have tried all sorts of way of doing this but nothing seems to work. Please see code below for example of some of them. Unfortunately I cannot view the error log on my system as I have no access to switch this on with the ISP/Host
Code: Select all
// Reads information from data file
$handle = fopen("morale_data.csv",r);
if ($handle) {
while(!feof($handle)){
$line=fgets($handle);
list($morale1,$morale2,$morale3,$morale4,$morale5,$morale6) = explode(",","$line");
$morale_array1[]=$morale1;
$morale_array2[]=$morale2;
$morale_array3[]=$morale3;
$morale_array4[]=$morale4;
$morale_array5[]=$morale5;
$morale_array6[]=$morale6;
}
fclose($handle);
}
// resets and sorts all basic imported arrays
reset($morale_array1);
reset($morale_array2);
reset($morale_array3);
reset($morale_array4);
reset($morale_array5);
reset($morale_array6);
sort($morale_array1);
sort($morale_array2);
sort($morale_array3);
sort($morale_array4);
sort($morale_array5);
sort($morale_array6);
// Adds the identical values within each array to produce a count for each answer
$morale_count1=array_count_values($morale_array1);
$morale_count2=array_count_values($morale_array2);
$morale_count3=array_count_values($morale_array3);
$morale_count4=array_count_values($morale_array4);
$morale_count5=array_count_values($morale_array5);
$morale_count6=array_count_values($morale_array6);
// Produces the total number of not null answers for each question. This does not use count as fwrite in process always produces a blank line at the end of the file giving dummy null readings.
$total_morale1=($morale_count1[1]+$morale_count1[2]+$morale_count1[3]+$morale_count1[4]+$morale_count1[5]);
$total_morale2=($morale_count2[1]+$morale_count2[2]+$morale_count2[3]+$morale_count2[4]+$morale_count2[5]);
$total_morale3=($morale_count3[1]+$morale_count3[2]+$morale_count3[3]+$morale_count3[4]+$morale_count3[5]);
$total_morale4=($morale_count4[1]+$morale_count4[2]+$morale_count4[3]+$morale_count4[4]+$morale_count4[5]);
$total_morale5=($morale_count5[1]+$morale_count5[2]+$morale_count5[3]+$morale_count5[4]+$morale_count5[5]);
$total_morale6=($morale_count6[1]+$morale_count6[2]+$morale_count6[3]+$morale_count6[4]+$morale_count6[5]);
// Produce the % array of each array for use in 2nd report
$percent_morale_count1=percentCalc($morale_count1,$total_morale1);Code: Select all
<?php
function percentCalc($array,$total)
{
while (list($key,$arraycontent)= each($array)
{
$c_percent=(($arraycontent/$total)*100);
$final_array[]=$c_percent;
}
$final_array=$array;
}
?>