Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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

Comments

I don't know about the array_slice() function yet so I came up with this:

sort($temp);
echo "The warmest five high temperatures were:<br/>";
for($x=29; $x>=25; $x--){
	echo "$temp[$x] &degF <br/>\n";
}

echo "<p>The coolest five high temperatures were:<br/>\n";
for($x=4; $x >= 0; $x--){
	echo "$temp[$x] &degF <br/>\n";
}

thank you for this exercise, I'm learning a lot.

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!

This is what I came up with

  1. 	<h2>Manipulate the Array</h2>
  2. 	<h3>High Temperatures for Spring Month</h3>
  3. <?php
  4. $temp = array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78,
  5.  68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83);
  6.  
  7.  //Add the values in the array
  8.  $sum = array_sum($temp);
  9.  //Count the items in the array
  10.  $count = count($temp);
  11.  //Find the average
  12.  $average = ($sum/$count);
  13.  //Remove duplicate temp
  14.  $temp = array_unique($temp);
  15.  //Sort array from low to high
  16.  sort($temp);
  17.  
  18.  //Slice array for last 5 temp
  19.  $warmest = array_slice($temp, -5, 5);
  20.  //Slice array for first 5 temp
  21.  $coolest = array_slice($temp, 0, 5);
  22.  
  23.  ?>
  24.  <p>The average high temp is: <?php echo round($average)."&degF"; ?></p>
  25.  <p>The 5 warmest high temps were: <?php foreach($warmest as $w){echo "$w&degF, ";} ?></p>
  26.  <p>The 5 coolest high temps were: <?php foreach($coolest as $c){echo "$c&degF, ";} ?></p>

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

  1. <html>
  2.  
  3. <head>
  4. 	<title>High Temperatures Array</title>
  5. </head>
  6.  
  7. <body>
  8.  
  9. <h2>High Temperatures for Spring Month</h2>
  10. <?php
  11. error_reporting(E_ALL ^ E_NOTICE);
  12. if (!isset($_POST["submit"]))
  13. 	{
  14. ?>
  15. <p>Please give us the 30 Fahrenheit high temperatures for a spring month separated by comma</p>
  16. <form method="post" action="">
  17. 	<input type="text" name="temp" size="80"></input><br>
  18. 	<input type="submit" name="submit" value="submit"></input><br>
  19. </form>
  20.  
  21. <p>The average high temperature for the month was:</p>
  22. <p>The warmest five high temperatures were:</p>
  23. <p>The coolest five high temperatures were:</p>
  24.  
  25. <?php
  26. 	}
  27. else
  28. 	{
  29. 	$temp = explode(',', $_POST["temp"]);
  30. 	$count = count($temp);
  31. 	settype($max5, "array");
  32. 	settype($min5, "array");
  33. 	foreach ($temp as $value)
  34. 		{
  35. 			settype($sum, "float");
  36. 			$sum += $value;
  37. 		}
  38. 	$aver = round($sum / $count);
  39. 	include 'maxn_function.php';
  40. 	$max5 = maxn(5, $temp);
  41. 	include 'minn_function .php';
  42. 	$min5 = minn(5, $temp);
  43. ?>
  44. <p>Please give us the 30 Fahrenheit high temperatures for a spring month separated by comma</p>
  45. <form method="post" action="">
  46. 	<input type="text" name="temp" size="80"></input><br>
  47. 	<input type="submit" name="submit" value="submit"></input><br>
  48. </form>
  49.  
  50. <p>The average high temperature for the month was: <?php echo $aver . "&degF";?></p>
  51. <p>The warmest five high temperatures were:<?php echo "<br>";
  52. foreach ($max5 as $t => $value)
  53. 	{
  54. 		$intm = $max5[$t];
  55. 		echo $intm . "&degF" . "<br>";
  56. 	}
  57. ?></p>
  58. <p>The coolest five high temperatures were: <?php echo "<br>";
  59. foreach ($min5 as $t => $value)
  60. 	{
  61. 		$intm = $min5[$t];
  62. 		echo $intm . "&degF" . "<br>";
  63. 	}
  64. ?></p>
  65.  
  66. <?php
  67. 	}
  68. ?>
  69. </body>
  70.  
  71. </html>

maxn_function.php

  1. <?php
  2.  
  3. function maxn($n, $temp)
  4. 	{
  5. 		settype($maxn, "array");
  6. 		$count = count($temp);
  7. 		for ($z = 0; $z < $n; $z++)
  8. 			{
  9. 				$maximum = 0;
  10. 				for ($i = 0; $i < $count; $i++)
  11. 					{	
  12. 						if ($maximum < $temp[$i])
  13. 							{
  14. 								$maximum = $temp[$i];
  15. 							}
  16. 					}
  17. 				$count--;
  18. 				$key = array_search($maximum, $temp);
  19. 				array_push($maxn, $maximum);
  20. 				unset($temp[$key]);
  21. 				$temp = array_values($temp);
  22. 			}
  23.  
  24. 		return $maxn;
  25. 	}
  26.  
  27. ?>
  28.  
  29. {/syntaxhighlighter}
  30.  
  31.  
  32. minn_function .php
  33. {syntaxhighlighter brush:php;collapse:true;}
  34.  
  35.  
  36. <?php
  37.  
  38. function minn($n, $temp)
  39. 	{
  40. 		settype($minn, "array");
  41. 		$count = count($temp);
  42. 		for ($z = 0; $z < $n; $z++)
  43. 			{
  44. 				$minimum = 10000000;
  45. 				for ($i = 0; $i < $count; $i++)
  46. 					{	
  47. 						if ($minimum > $temp[$i])
  48. 							{
  49. 								$minimum = $temp[$i];
  50. 							}
  51. 					}
  52. 				$count--;
  53. 				$key = array_search($minimum, $temp);
  54. 				array_push($minn, $minimum);
  55. 				unset($temp[$key]);
  56. 				$temp = array_values($temp);
  57. 			}
  58.  
  59. 		return $minn;
  60. 	}
  61.  
  62. ?>

Here is a solution without using the sort or splice functions:

  1. <!DOCTYPE html>
  2. <html>
  3. 	<head>
  4. 	<meta charset="utf-8"  />
  5. 		<title>le PHP Study</title>
  6. 	</head>
  7.  
  8. 	<body>
  9. 		<p>
  10. 			<?php
  11. 				// Prints the elements of an array comma-separated.
  12. 				function PrintArray( $a, $name )
  13. 				{
  14. 					echo( "<h2>$name</h2>" );
  15. 					if( count($a) == 0 )
  16. 						return;
  17. 					for( $i=0; $i<count($a); $i++ )
  18. 						if( $i == 0 )
  19. 							echo( "$a[$i]" );
  20. 						else
  21. 							echo( ", $a[$i]" );
  22. 					echo( "<br />" );
  23. 				}
  24.  
  25.  
  26. 				$temps = array(
  27. 					68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78,
  28. 					68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83 );
  29.  
  30.  
  31. 				// Initialize the arrays of warmest and coolest temps with
  32. 				// the first 5 temps from the list. Then, for each t in temps,
  33. 				// if it is warmer than any temp in the warmest temps array, replace it.
  34. 				// If it is cooler than any temp in the coolest temps array, replace it.
  35.  
  36. 				for( $i=0; $i<5; $i++ )
  37. 				{
  38. 					$warmest[$i] = $temps[$i];
  39. 					$coolest[$i] = $temps[$i];
  40. 				}
  41.  
  42. 				// Start from the sixth temp because the first five have already been added.
  43. 				for( $it=5; $it<count($temps); $it++ )
  44. 				{
  45. 					$t = $temps[$it];
  46. 					for( $i=0; $i<count($warmest); $i++ )
  47. 					{
  48. 						if( $t > $warmest[$i] )
  49. 						{
  50. 							$warmest[$i] = $t;
  51. 							break;
  52. 						}
  53.  
  54. 						if( $t < $coolest[$i] )
  55. 						{
  56. 							$coolest[$i] = $t;
  57. 							break;
  58. 						}
  59. 					}
  60. 				}
  61. 				PrintArray( $temps, "Temperatures" );
  62.  
  63. 				// Calculate and print the average of the temperatures.
  64. 				$t_total = 0;
  65. 				foreach( $temps as $t )
  66. 					$t_total += $t;
  67. 				$t_average = $t_total / count($temps);
  68. 				printf( "Average temperature is %.2f&deg;<br />", $t_average );
  69.  
  70. 				PrintArray( $warmest, "Warmest 5 Temperatures" );
  71. 				PrintArray( $coolest, "Coolest 5 Temperatures" );
  72. 			?>
  73. 		</p>
  74.  
  75. 	</body>
  76. </html>