PHPBuilder - Arrays 101 Page 2



RSS Twitter
Articles Php Functions

Arrays 101 - Page 2

by: Joshua Petrovich
|
April 24, 2003

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.

« Previous Page
1
|
2

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Joshua Petrovich

Comment:



Comment:

(Maximum characters: 1200). You have characters left.