Jump to content

[SOLVED] calculating avarages


kellz

Recommended Posts

$people = array(
              'peter' => 22,
		  'markus' => 17,
		  'andrew' => 18,
		  'steph'=> 22,
		  'jhon' =>  22,
		  'anna' => 18
		  );

foreach ($people as $name => $age) {
echo "$name is $age years old.<br>";
}
echo '<br>';

$ages = 22*3+17+18*2;
$avg = $ages / 6;
echo "the avarage age is " . substr($avg,0,2);

 

i need a better way to do this, because that is a set array but when i finish i wont know the ages of the people... i guessed loooooping through the array and getting the value and adding them together then deviding the total but ugh it just went all messy lol.

 

and btw, this looks like something someone would do for homework but unfortunately my school doesn't offer nothing more than basic IT (word/excel/powerpoint)..ect..  :'(

Link to comment
https://forums.phpfreaks.com/topic/76419-solved-calculating-avarages/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.