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.. :'( Quote Link to comment Share on other sites More sharing options...
Barand Posted November 7, 2007 Share Posted November 7, 2007 $av_age = array_sum($people) / count($people); Quote Link to comment Share on other sites More sharing options...
kellz Posted November 7, 2007 Author Share Posted November 7, 2007 thanx you! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 If this is solved, click "Topic solved" Quote Link to comment Share on other sites More sharing options...
kellz Posted November 8, 2007 Author Share Posted November 8, 2007 sry, i always forget that^^ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.