Mar
21
2008

Multidimensional Arrays

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 =&gt; 'Milk','Cheese','Yogurt');//Our second array is going to be for animals:
$animals = array(1 =&gt; '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' =&gt; $dairy, 'Animals' =&gt; $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.

Written by Kevin in: PHP, WebDev |

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes