Ok…I skipped a day - but guess I’m still not back to 100% - I was just exhausted - really odd for me - and I laid on the sofa all afternoon….and evening…now - that’s getting old and it’s time to move on!
Creating Multi-Dimensional Arrays
Hoo boy - this sounds complicated….I see that word “nesting” again….fraught with danger! But, hey, nothing ventured, nothing gained - so let’s see if I can understand it….and DO it!
A multi-dimensional array is used so you can store more information than you can in a regular array. An array with elements that are also arrays. Can see where this could get very confusing - so going slow here.
Example: $fruits = array(“apples”, “bananas”, “oranges”);
$meats = array(“steaks”, “hamburger”, “pork chops”);
$groceries = array (
“fruits” => $fruits,
“meats” => $meats,
“other” => “peanuts”,
“cash” => 30.00
);
So…this is an array called “groceries” and it has four elements - the first one is another array of “fruits”, the second one is an array of “meats”, the third one is
a string “peanuts”, and the fourth element is a floating point number “30.00”.
Ok….I understand that!
Now - to point to an item within this array of arrays is a little tricky. So…you move from the outside in, identifying with square brackets where you want to go. In the above example, if I wanted to point to “bananas”:
$groceries[ “fruits” ] [ 1 ].
First, I’m pointing to the element - in this case the array “fruits”, and then to the element within that array, and since we always count from 0 - it is “1”. Ok…this makes perfect sense too. Kinda like nesting HTML tags.
Now to use this information. The book uses the example of books and chapters - I’m sticking with food - making up my own multi-dimensional array. Be interesting to see if my version works.
First - create the first array - appetizers
$appetizers = array ( "blooming onion", "Cheese Fries", "Mozzarella Sticks");
Now - the second array - entrees
$entrees = array ( "New York Strip Steak", "Roast Beef", "Shrimp Scampi", "Baked Chicken");
And…the third array - desserts
$desserts = array ( "Fudge Brownie", "Vanilla Ice Cream", "Warm Apple Pie");
And now…the main, multi-dimensional array:
$menu = array (
"appetizers" => $appetizers,
"entrees" => $entrees,
"desserts" => $desserts
);
Remembering to put a comma between elements, except for the last one, and to put a semi-colon at the end of the array.
Now - to print out some of the values, so I can see that it is working.
print "The third entree on my menu is { $menu [ "entrees" ] [ 2 ]}";
This should print out “Shrimp Scampi”
print "The first dessert on my menu is { $menu [ "desserts" ] [ 0 ]}";
This should print out “Fudge Brownie”.
Now do a “foreach” loop to print the whole thing:
foreach ($menu as $key => $value) {
print "$key: $value\n";}
Now….crunch time - will it work? Saved - uploaded - test….Nope….dreaded white blank page. What did I do wrong?
Commenting out all but the first print command - see if that works NO….so I’ve done something wrong before that.
Worked with just the first print command - and printed out the correct menu item. Now to check and add back the second one. Ok - that worked. Seemed I had too much spacing between things - and I capitalized the words in my master array - changed that. And now it works.
Didn’t print out every element of every array - I thought it would - but that’s later in the “tips” - but my 15 for today is gone - do that tomorrow ‘cause I want to know how!
You can check out the latest progress here:
Recent Comments
Please login to comment.

PandoraWilde said (4 months ago)