Jump to content

Using Multidimensional Array


Go to solution Solved by Barand,

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/283985-using-multidimensional-array/
Share on other sites

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?

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 :o)

  • Solution

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]

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  :tease-01:  I am sure I will be back again for further education ...

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.