Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Arrays 101
Multi-dimensional arrays. Sounds like it came from some 1950s Sci-Fi novel. Anywho, multi-dimensional arrays are simply arrays within arrays. You remember eariler how I was putting variables into arrays? Well now I'm stuffing arrays into arrays. For example, let's say I run an on-line bookstore and I sell novels, kid's books, and magazines, and I put the names of those into arrays. In code, it'd look like:

<?php
    $arrayNovels
= array("Andromeda Strain", "Rainbow Six", "Lord of the Rings");
    
$arrayKidBooks = array("Jack and Jill", "Clifford the Big Red Dog", "How Stuff Works");
    
$arrayMagazines = array("Motorcycle World", "Guitar One", "Car and Driver");
?>
Now this is fairly condensed, but I can stuff all three arrays into an array called $arrayInventory. Using what I've already talked about, this is how it's done:

<?php
    $arrayNovels
= array("Andromeda Strain", "Rainbow Six", "Lord of the Rings");
    
$arrayKidBooks = array("Jack and Jill", "Clifford the Big Red Dog", "How Stuff Works");
    
$arrayMagazines = array("Motorcycle World", "Guitar One", "Car and Driver");

    
$arrayInventory = array();
    
$arrayInventory["arrayNovels"] = $arrayNovels; // this is the same as index 0
    
$arrayInventory["arrayKidBooks"] = $arrayKidBooks; // this is the same as index 1
    
$arrayInventory["arrayMagazines"] = $arrayMagazines; // this is the same as index 2
?>
So now I can print them out using for-loops (assume that the above code is included):

<?php
    $nSizeInv
= count($ArrayInventory);
    for(
$indexInv=0; $indexInv < $nSizeInv; $indexInv++)
    {
        if(
$indexInv==0) // remember that index 0 is the array for novels
        
{
            
$arrayNovels = $ArrayInventory[$index];
            
$nSizeNovels = count($arrayNovels);
            for(
$indexNov=0; $indexNov < $nSizeNovels; $indexNov++)
            {
                
$sNovel = $arrayNovels[$indexNov];
                print
"Novel: $sNovel";
            }
        }
        if(
$indexInv==1) // remember that index 1 is the array for kid's books
        
{
            
$arrayKidsBooks = $ArrayInventory[$index];
            
$nSizeKbooks = count($arrayKidsBooks);
            for(
$indexKbook=0; $indexKbook < $nSizeKbooks; $indexKbook++)
            {
                
$sKidsBook = $arrayKidsBooks[$indexKbook];
                print
"Kids book: $sKidsbook";
            }
        }
        if(
$indexInv==2) // remember that index 2 is the array for magazines
        
{
            
$arrayMagazines = $ArrayInventory[$index];
            
$nSizeMags = count($arrayMagazines);
            for(
$indexMags=0; $indexMags < $nSizeMags; $indexMags++)
            {
                
$sMagazine = $arrayMagazines[$indexMags];
                print
"Magazine: $sMagazine";
            }
        }
    }
?>
So there you have it. You can now create, fill, and use arrays for all your scripting needs. Drop me a line at the address above if you have a specific question about arrays or PHP in general. Thanks, and may the code be with you.

[Page 1]  [Page 2]  


Comments:
array storage in MySQlJones02/14/06 12:22
i need help on all of PHPSeth03/09/05 15:26
tutorialv maurice02/09/05 06:13
RE: Foreachoiram08/14/03 21:54
print_rStephen VanDyke07/02/03 18:16
Excellent TipGohar06/07/03 04:49
better articleDmitriy A. Nesterkin04/30/03 11:46
array's are gotoohio04/30/03 06:46
RE: Grr..... :-(Kevin J. Fry04/29/03 22:35
The => syntax is missingKaare04/27/03 09:45
ForeachChris Travers04/26/03 15:48
Grr..... :-(Alex Gorsky04/25/03 17:11
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.

 

Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs