kellz Posted November 7, 2007 Share Posted November 7, 2007 $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 More sharing options...
Barand Posted November 7, 2007 Share Posted November 7, 2007 $av_age = array_sum($people) / count($people); Link to comment https://forums.phpfreaks.com/topic/76419-solved-calculating-avarages/#findComment-386990 Share on other sites More sharing options...
kellz Posted November 7, 2007 Author Share Posted November 7, 2007 thanx you! Link to comment https://forums.phpfreaks.com/topic/76419-solved-calculating-avarages/#findComment-387006 Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 If this is solved, click "Topic solved" Link to comment https://forums.phpfreaks.com/topic/76419-solved-calculating-avarages/#findComment-387229 Share on other sites More sharing options...
kellz Posted November 8, 2007 Author Share Posted November 8, 2007 sry, i always forget that^^ Link to comment https://forums.phpfreaks.com/topic/76419-solved-calculating-avarages/#findComment-387548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.