Multidimensional arrays are basically arrays within arrays. So, let's make one.
<? //Our first array is going to be for dairy products: $dairy = array(1 => 'Milk','Cheese','Yogurt');//Our second array is going to be for animals: $animals = array(1 => 'Cow','Duck','Horse','Chicken'); //Now let's pretend we wanted to combine these arrays (I should've chosen something like Months and Years or States and Territories...but I'm sticking with dairy products and farm animals ;) ....) $combined = array('Dairy' => $dairy, 'Animals' => $animals); //Idea: This is the best way to categorize interests for personalized profiles etc.... //Echoing what we've just done... echo "I want to echo Cow! {$combined['animals']['Cow']}"; ?>
Of course, if you're just wanting to echo Cow, just use echo "Cow";...but if you're building things from arrays like drop down menus or if you're collecting interests, this is helpful.
This entry was posted on Friday, March 21st, 2008 at 9:50 pm and is filed under Blog, PHP, WebDev. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get a Trackback link
No Comments Yet
You can be the first to comment!
Leave a comment