Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

26

May

PHP Array question number #6 from phpexercises.com

There is a famous quote on practice that goes like this “Practice is the best of all instructors”. Practice is the best way to learn. In this post, we will solve a question from http://phpexercises.com/php-manipulate-array-exercise.html titled “PHP Arrays Ex. #6: Manipulate the Array”. I came up with my own version and then I compared it to what they had come up with. Then I went further into making it more presentable by changing a few things. Let’s go over it. First read the question from the website.

They want you to choose a spring month because spring months have 30 days. However, does this really matter? No. Why? Well, we know for sure that have 30 days to work with. That is all we need to worry about. You don’t have specify any month on your script. It’s just information to try to complicate more. Most questions become tricky when they try to throw useless information. To simplify it, don’t worry about Celsius temps. Just keep it simple. The temps are already in Fahrenheit so keep it that way. We already ruled out 2 pieces of unnecessary information. No month, no Celsius. The key to solving this problem lies on the second paragraph. You need to compute the average of all high temps. Remember, all of those temps are HIGH temps.  So to calculate the mean value(average) sum them up and divide by 30. Next, find the 5 warmest temps and the 5 coolest temps. You got 3 tasks to do on this question. Before coding, take a pen and a paper and do some brainstorming. I wrote down all the numbers that they mentioned on the question. I computed the average and then I re-arranged the values so that I could better see which ones were the highest numbers(warmest) and which ones were the lowest numbers(coolest). So right off the bat, you have the first answer. All you got to do is add and divide to get the average. Then for the other 2 answers, re-arrange(sort) the array so that you can pick the 5 lowest and 5 warmest temps. Job Done. After the initial brainstorming, we go into actual coding.

The code explains it all so take a look. If you don’t know what the functions do then consult the PHP manual. This is what I came up with on my own:

<?php
/*
PHP Arrays Ex. #6: Manipulate the Array

In this PHP exercise, you will create an array of temperatures. Choose a spring month to have a wider range of
temperatures to handle. We’ll use 30 days of the month. The exercise is generic, but feel free to use a specific
month in your own script. The answer script will use the Fahrenheit scale, but again feel free to use Celsius if you prefer.

Create your array of 30 high temperatures, approximating the weather for a spring month, then find the average
high temp, the five warmest high temps and the five coolest high temps. Print your findings to the browser.

Hint: the HTML character entity for the degree sign is &deg;.

Feel free to make up the temps or gather data for your own area. Here’s a list of thirty Fahrenheit high temperatures
you can use if you like:

68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78,
68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83
*/

// Create an array of temperatures for the month of March
$monthTemp = array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78, 68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83);

// Finding the average for high temp. Take all numbers and do the average.
    $count = count($monthTemp);
    $total = 0;
    for($a = 0; $a < $count; $a++) {
        $sum = $monthTemp[$a] . ‘<br />’;
        $total += $sum;
    }
    $average = $total / $count;
    echo ‘The average for high temp is ’ . $average . ’ &deg;F or ’ . round($average) . ’ &deg;F<br /><br />’;

// The five warmest high temps. 80, 82, 83, 85, 89. We know that the highest temperature is 89.
    // We sort the array to have the last 5 numbers be the warmest high temps.
    sort($monthTemp);
   
    // pop last 5 keys
    $highTemp5 = array_pop($monthTemp);
    $highTemp4 = array_pop($monthTemp);
    $highTemp3 = array_pop($monthTemp);
    $highTemp2 = array_pop($monthTemp);
    $highTemp1 = array_pop($monthTemp);
   
    echo ‘The five warmest high temps are: ’ . $highTemp5 . ‘&deg;F, ’ . $highTemp4 . ‘&deg;F, ’ . $highTemp3 . ‘&deg;F, ’ . $highTemp2 . ‘&deg;F, ’ . $highTemp1 . ‘&deg;F<br /><br />’;

// The five coolest high temps.    
    $coolestTemp5 = array_shift($monthTemp);
    $coolestTemp4 = array_shift($monthTemp);
    $coolestTemp3 = array_shift($monthTemp);
    $coolestTemp2 = array_shift($monthTemp);
    $coolestTemp1 = array_shift($monthTemp);
   
    echo ‘The five coolest high temps are: ’ . $coolestTemp5 . ‘&deg;F, ’ . $coolestTemp4 . ‘&deg;F, ’ . $coolestTemp3 . ‘&deg;F, ’ . $coolestTemp2 . ‘&deg;F, ’ . $coolestTemp1 . ‘&deg;F<br /><br />’;
?>

A bit more complicated than the original answer but I was able to get to the same answer like they did. Now look at the original answer from their site and compare it to mine:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
     “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 
<html xmlns=”http://www.w3.org/1999/xhtml”  xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”content-type” content=”text/html;charset=iso-8859-1” />
<title>High Temperatures Array</title>
</head>
 
<body>
<h2>High Temperatures for Spring Month</h2>
 
<?php
//Create an array of 30 Fahrenheit high temperatures for a spring month.
$highTemps = array(
  68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78,
  68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83
);
 
//Get number of temps.
$count = count($highTemps);
 
//Get a total of all temps.
$total = 0;
foreach ($highTemps as $h){
  $total += $h;
}
 
//Calculate average.
$avg = round($total / $count);
 
//Send data to the browser. &deg; is the ASCII code for the degree sign.
echo “<p>The average high temperature for the month was $avg &deg;F.</p>\n”;
 
//Sort the temps and get the top and bottom five.
//Use rsort to produce a descending sort.
rsort($highTemps);
//Pull out the top 5 temps.
$topTemps = array_slice($highTemps, 0, 5);
echo “<p>The warmest five high temperatures were: <br />\n”;
foreach($topTemps as $t){
  echo “$t &deg;F <br/> \n”;
}
echo “</p>”;
  
//Pull out the bottom five temps.
$lowTemps = array_slice($highTemps, -5, 5);
echo “<p>The coolest five high temperatures were: <br/>\n”;
foreach($lowTemps as $l){
  echo “$l &deg;F <br/> \n”;
}
echo “</p>”;
 
?>
 
</body>
</html>

In trying to play with it and further my knowledge, I got rid of duplicates keys from the array, added averages for the 5 warmest and coolest temps and then made sure that the coolest temps printed ascending rather. But remember, this is not exactly what they asked of you to do. So it would have been wrong to give this as an answer.

<?php
/*
PHP Arrays Ex. #6: Manipulate the Array

In this PHP exercise, you will create an array of temperatures. Choose a spring month to have a wider range of
temperatures to handle. We’ll use 30 days of the month. The exercise is generic, but feel free to use a specific
month in your own script. The answer script will use the Fahrenheit scale, but again feel free to use Celsius if you prefer.

Create your array of 30 high temperatures, approximating the weather for a spring month, then find the average
high temp, the five warmest high temps and the five coolest high temps. Print your findings to the browser.

Hint: the HTML character entity for the degree sign is &deg;.

Feel free to make up the temps or gather data for your own area. Here’s a list of thirty Fahrenheit high temperatures
you can use if you like:

68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78,
68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83
*/

// Create an array of temperatures for the month of March
$monthTemp = array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78, 68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83);

// Finding the average for high temp. Take all numbers and do the average.
    $count = count($monthTemp);
    $total = 0;
    for($a = 0; $a < $count; $a++) {
        $sum = $monthTemp[$a] . ‘<br />’;
        $total += $sum;
    }
    $average = $total / $count;
    echo ‘The average for high temp is ’ . $average . ’ &deg;F or ’ . round($average) . ’ &deg;F<br /><br />’;

// The five warmest high temps. 80, 82, 83, 85, 89. We know that the highest temperature is 89.
    // We sort the array to have the last 5 numbers be the warmest high temps.
    sort($monthTemp);
   
    // We get rid of duplicated   
    $noDuplicates = array_unique($monthTemp);

    // pop last 5 keys
    $highTemp5 = array_pop($noDuplicates);
    $highTemp4 = array_pop($noDuplicates);
    $highTemp3 = array_pop($noDuplicates);
    $highTemp2 = array_pop($noDuplicates);
    $highTemp1 = array_pop($noDuplicates);
   
    echo ‘The five warmest high temps are: ’ . $highTemp5 . ‘&deg;F, ’ . $highTemp4 . ‘&deg;F, ’ . $highTemp3 . ‘&deg;F, ’ . $highTemp2 . ‘&deg;F, ’ . $highTemp1 . ‘&deg;F<br /><br />’;
    $warmestAverage = ($highTemp5 + $highTemp4 + $highTemp3 + $highTemp2 + $highTemp1) / 5;
    echo ‘The average of the five warmest high temps are: ’ . $warmestAverage . ’ &deg;F or ’ . round($warmestAverage) . ’ &deg;F<br /><br />’;

// The five coolest high temps.    
    $coolestTemp5 = array_shift($noDuplicates);
    $coolestTemp4 = array_shift($noDuplicates);
    $coolestTemp3 = array_shift($noDuplicates);
    $coolestTemp2 = array_shift($noDuplicates);
    $coolestTemp1 = array_shift($noDuplicates);
   
    echo ‘The five coolest high temps are: ’ . $coolestTemp5 . ‘&deg;F, ’ . $coolestTemp4 . ‘&deg;F, ’ . $coolestTemp3 . ‘&deg;F, ’ . $coolestTemp2 . ‘&deg;F, ’ . $coolestTemp1 . ‘&deg;F<br /><br />’;
    $coolestAverage = ($coolestTemp5 + $coolestTemp4 + $coolestTemp3 + $coolestTemp2 + $coolestTemp1) / 5;
    echo ‘The average of the five warmest high temps are: ’ . $coolestAverage . ’ &deg;F or ’ . round($coolestAverage) . ’ &deg;F<br /><br />’;
?>