fintanstrasbourg Posted March 1, 2017 Share Posted March 1, 2017 Hi there, Been trying to learn PHP in my spare time the last few weeks. Setting myself little tasks, challenges, mini projects etc. but I'm quite stuck on this one. I'm trying to create an associative array to hold the following information on monthly rainfall in millimetres (jpeg of table attached below). I then want to find the minimum, maximum and average rainfall per month and per year. I've created two arrays experimenting but only the second one is associative (I've attached the php file below as well), but here is the code anyways... <?php $multidimensional_array = array(array("Jan", 292.9, 147.1, 175.9, 130.6, 107.7), array("Feb", 53.7, 154.9, 195.1, 91.1, 144.6), array("Mar", 92.8, 72.3, 87.2, 106.8, 64.4), array("Apr", 64.2, 55.4, 38.3, 63.6, 42.0), array("May", 135.3, 128.6, 90.4, 111.2, 45.4), array("June", 37.1, 63.4, 40.5, 27.2, 68.0), array("July", 76.5, 31.6, 35.8, 82.7, 66.9), array("Aug", 97.1, 153.8, 90.0, 93.4, 107.4), array("Sep", 39.2, 16.1, 72.4, 87.9, 88.5), array("Oct", 57.6, 125.2, 105.2, 49.7, 35.3), array("Nov", 184.4, 154.4, 129.3, 190.5, 69.2), array("Dec", 274.3, 100.7, 110.2, 233.2, 74.0), ); $multidimensionalAss_array = array( array("month"=>"Jan", "2012"=>92.9, "2013"=>147.1, "2014"=>175.9, "2015"=>130.6, "2016"=>107.7), array("month"=>"Feb", "2012"=>53.7, "2013"=>154.9, "2014"=>195.1, "2015"=>91.1, "2016"=>144.6), array("month"=>"Mar", "2012"=>92.8, "2013"=>72.3, "2014"=>87.2, "2015"=>106.8, "2016"=>64.4), array("month"=>"Apr", "2012"=>64.2, "2013"=>55.4, "2014"=>38.3, "2015"=>63.6, "2016"=>42.0), array("month"=>"May", "2012"=>135.3, "2013"=>128.6, "2014"=>90.4, "2015"=>111.2, "2016"=>45.4), array("month"=>"June", "2012"=>37.1, "2013"=>63.4, "2014"=>40.5, "2015"=>27.2, "2016"=>68.0), array("month"=>"July", "2012"=>76.5, "2013"=>31.6, "2014"=>35.8, "2015"=>82.7, "2016"=>66.9), array("month"=>"Aug", "2012"=>97.1, "2013"=>153.8, "2014"=>90.0, "2015"=>93.4, "2016"=>107.4), array("month"=>"Sep", "2012"=>39.2, "2013"=>16.1, "2014"=>72.4, "2015"=>87.9, "2016"=>88.5), array("month"=>"Oct", "2012"=>57.6, "2013"=>125.2, "2014"=>105.2, "2015"=>49.7, "2016"=>35.3), array("month"=>"Nov", "2012"=>184.4, "2013"=>154.4, "2014"=>129.3, "2015"=>190.5, "2016"=>69.2), array("month"=>"Dec", "2012"=>274.3, "2013"=>100.7, "2014"=>110.2, "2015"=>233.2, "2016"=>74.0), ); //print_r($multidimensional_array); print_r($multidimensionalAss_array); ?> Could anyone give me some advice on where I'd go next with the associative array? I'm fairly sure I can't loop through it no? I'm fairly stuck on this and it's annoying me quiet a bit! Finding JavaScript easier to learn for sure... haha Thanks, Fintan table challenge.php Link to comment Share on other sites More sharing options...
Ingolme Posted March 1, 2017 Share Posted March 1, 2017 PHP has an iteration loop called foreach(). You can go through elements in any kind of array as in the following demonstration. <?php foreach($array as $index=> $item) { ?> <tr> <?php foreach($item as $key => $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> Link to comment Share on other sites More sharing options...
fintanstrasbourg Posted March 2, 2017 Author Share Posted March 2, 2017 Thanks very much for the help Foxy Mod it's greatly appreciated indeed... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now