"; echo "
$theData


"; // The above data dump isn't pretty or useful. Except that it gives you a quick view of what is in the data file. // To work with those data in a meaningful way, we have to employ some tricks. /* In PHP the data are read in as one super-long string. Thus, we first need to "explode" by rows (linebreak) to separate each person's data from the next. Then we "explode" by elements within that array to more classically pull out the individual data point. */ // explode each row by linebreak (\n) /* The result of this operation is to create an array ($theData_byrow) that contains a separate element for each row in the data file. Because each row represents a different person's data, this operation is the functioanl equivalent of creating an array for which each element represents the data for a different person. */ $theData_byrow = explode("\n",$theData); print "

Here is the content of the data file, represented in the array \$theData_byrow. Notice that each element of the array contains a string that represents the ENTIRE ROW of data for a person. Within each folder in the array, we have the information we need (separated by commas), but it is all represented as a single string. We'll need to explode the data one more time (within persons) to extract the comma-delimited information.

"; print_r($theData_byrow); print "

Let's try to break this down within a person by using a foreach loop to sort through each element of the array. We will print the 3rd element for each person. (Don't forget that PHP starts counting at 0.) This value in the dataset represents the person's rating of the photo on a 1 to 5 scale.

"; foreach($theData_byrow as $key => $value){ $theData_thisPerson = explode(",",$value); print "The 3rd value for row $key is $theData_thisPerson[3].
"; } /* Ok, now the fun begins. Let's create a sample average. We will do so by using a loop. As we loop through each person's data, we'll explode it and pull out the 3rd value. We will add that value to a running sum ($sum). We will also increment our sample size by 1 ($n). When the loop is complete, we will simply divide $sum by $n to get our sample average. Presto! A sample mean. */ $n = 0.00000001; $sum = 0; foreach($theData_byrow as $key => $value){ $theData_thisPerson = explode(",",$value); $sum = $sum + $theData_thisPerson[3]; $n = $n + 1; } $sample_mean = $sum/$n; $sample_mean = round($sample_mean,2); // round to 2 decimal places print "

The average rating for people is $sample_mean.

"; /* Some things to note: - This particular process can be made more efficient by creating a function to average things. Otherwise, we would have to do something like this fresh everytime we wish to average a variable. We'll discuss functions separately. - This particular code does not handle missing data well. We can do more sophisticated things to deal with missing data. But I don't want to overcomplicate things at this point. - Notice that we started with a predefined value for $n and $sum. This wasn't technically necessary. But, in some applications, it can be handy to start with n being slightly > 0 so you don't accidentally divide by 0 and anger the gods. */ ?>