daveasc Posted November 17, 2013 Share Posted November 17, 2013 Ok, new to PHP ... trying to figure out how to use a multidimensional array,the one shown below. The print_r works fine. But how do I use the array to show the values in another page, e.g. on the new page I want to display the "4" from the "Par" array and the "410" from the "BackYardage" array. Probably very simple but can't figure it out. Thanks. $teeboxes = array ( "Par" => array ("4","5","3","4","3","4","4","5","4","4","5","3","4","3","4","4","5","4"), "BackYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "MiddleYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "FrontYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "ForwardYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "SeniorYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), ); echo "<p> Dump of Tee Box array <p>"; print_r($teeboxes); Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/ Share on other sites More sharing options...
Barand Posted November 17, 2013 Share Posted November 17, 2013 Like this $teeboxes = array ( "Par" => array ("4","5","3","4","3","4","4","5","4","4","5","3","4","3","4","4","5","4"), "BackYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "MiddleYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "FrontYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "ForwardYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), "SeniorYardage" => array ("410","525","173","394","133","410","425","590","410","410","525","173","394","133","410","425","590","410"), ); /********************************************* * Display the data **********************************************/ for ($tee=1; $tee<=18; $tee++) { echo "<strong>Tee : $tee</strong><ul>"; foreach ($teeboxes as $desc => $data) { echo "$desc : {$data[$tee-1]}<br>"; } echo "</ul><br>"; } /* RESULTS Tee : 1 Par : 4 BackYardage : 410 MiddleYardage : 410 FrontYardage : 410 ForwardYardage : 410 SeniorYardage : 410 Tee : 2 Par : 5 BackYardage : 525 MiddleYardage : 525 FrontYardage : 525 ForwardYardage : 525 SeniorYardage : 525 ... etc */ But why not just pull it from your database on the page where you want to display the data? Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458648 Share on other sites More sharing options...
daveasc Posted November 17, 2013 Author Share Posted November 17, 2013 Thanks, Barand. It does as you say. But two things: 1. Can you explain what is going on? I am totally new to PHP, at least working with arrays. What is "$desc"? How does echo $desc : {$data[$tee-1]} work? Or where can I read up of this? 2. What I really want is to be able to take the "Par" and one of the teeboxes, e.g. "BackYardage" and put them into variables that I can display on a page. Then I could display any of the teeboxes. Hope this make sense to you. It seems like I should be able to do something like this: $hole1par = the 1st par, $hole2par = 2nd par, etc.. $hole1yardage = the 1st yardage for BackYardage, $hole2yardage = 2nd BackYardage, etc. I just don't know how to access any part of the array. Thanks ) Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458680 Share on other sites More sharing options...
Solution Barand Posted November 17, 2013 Solution Share Posted November 17, 2013 There is no point in putting the values from the array into 108 separate variable (6x18) When using foreach ($array as $key => $value) it loops through the array putting the key into $key (in this case the description ($desc) such as par, frontyardage etc) and the value into $value. In this case the value is an array of the 18 hole values. The indexes of these arrays is numeric from 0 to 17 (ie hole - 1). I used $tee for the hole number. So, if you want the par value for the 12th hole it would be in the variable $teeboxes['Par'][11] The FrontYardage for for 6th hole would be in $teeboxes['FrontYardage'][5] Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458682 Share on other sites More sharing options...
daveasc Posted November 17, 2013 Author Share Posted November 17, 2013 Thanks once again ... starting to come through little by little ... I had a general idea how it should work but it takes a long time to figure things out by "trial & error" ... mostly error I am sure I will be back again for further education ... Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458700 Share on other sites More sharing options...
daveasc Posted November 18, 2013 Author Share Posted November 18, 2013 Is it possible to "include" the array, $teeboxes, in a page on my site and use it just as if it was there all along? I mean, suppose I have that code in another php file named, "andersonyardages.php". I would like to be able to use the $teeboxes so I could use the data in the array without actually coding the data in a current file. Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458713 Share on other sites More sharing options...
cyberRobot Posted November 18, 2013 Share Posted November 18, 2013 Is it possible to "include" the array, $teeboxes, in a page on my site and use it just as if it was there all along? I mean, suppose I have that code in another php file named, "andersonyardages.php". I would like to be able to use the $teeboxes so I could use the data in the array without actually coding the data in a current file. See example 1 here: http://php.net/manual/en/function.include.php Quote Link to comment https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/#findComment-1458714 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.