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 °.
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
 
 
Here's the code:
<!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>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
To open a PHP code editor in a new tab, click here.
Comments
hi
I don't know about the array_slice() function yet so I came up with this:
thank you for this exercise, I'm learning a lot.
Use the array_sum() function
I also didn't know about the array_slice() function and used a similar solution, but I also used the array_sum() function to get the sum of the temps.
$avghi = (array_sum($temps))/(count($temps));
Great exercises! It's really helping me get to know PHP a lot better.
Thanks!
My Solution
This is what I came up with
<?php
//Add the values in the array
//Count the items in the array
//Find the average
//Remove duplicate temp
//Sort array from low to high
//Slice array for last 5 temp
//Slice array for first 5 temp
?>
Without using build-in functions
Please find here below my solution without using build-in functions. I have created three php files where the first one (main.php) includes the other two (maxn_function.php and minn_function.php).
main.php
<?php
{
?>
<?php
}
else
{
{
}
?>
{
}
?></p>
{
}
?></p>
<?php
}
?>
maxn_function.php
<?php
{
{
{
{
}
}
}
}
?>
<?php
{
{
{
{
}
}
}
}
?>
Without built-in array functions
Here is a solution without using the sort or splice functions:
<?php
// Prints the elements of an array comma-separated.
{
else
}
// Initialize the arrays of warmest and coolest temps with
// the first 5 temps from the list. Then, for each t in temps,
// if it is warmer than any temp in the warmest temps array, replace it.
// If it is cooler than any temp in the coolest temps array, replace it.
{
}
// Start from the sixth temp because the first five have already been added.
{
{
{
}
{
}
}
}
// Calculate and print the average of the temperatures.
?>