Arrays 101

类别:编程语言 点击:0 评论:0 推荐:

原作:Joshua Petrovich翻译:EasyChen

如果你曾经写过使用大量变量的脚本(有时候近100个),你就会知道跟踪每个变量的内容和用途是多么的难受。真的,我曾经有过这样的经历。如果我们能把变量保存在另一个变量里边,变量列表的长度就从100减少到10以内。这就是数组是如何而来的。

一个数组,从最简单的形式来讲,是一个保存变量的变量。这很像一个城市里的一列房屋。城市拥有很多房屋,而每个房屋有一个地址。同样的情况,每个变量(房子)在一个数组(城市)里有它自己的地址,我们叫它索引。

让我们假设你有三个人名分别存放在叫做$sPerson1、$sPerson2和$sPerson3的变量中。现在你可以在你的程序中使用这三个变量,但是这样很容易忘记哪个变量是哪个……尤其是有其他变量的时候。要将这三个变量放到一个数组中,你可以像这样做:

<?php
    $arrayPeople = array("John", "Susie", "Dave");
?>

现在,我使用$arrayPeople来代替$sPerson1, $sPerson2, and $sPerson3。注意我是如何使用PHP中的array()函数的。如果这三个名字是数字,我不会使用引号把它引起来。为了显示这三个名字,我是这样做的:

<?php
    $arrayPeople = array("John", "Susie", "Dave");
    print $arrayPeople[0];
    print $arrayPeople[1];
    print $arrayPeople[2];
?>

 

 

为什么是从零开始?因为索引从那里开始。不管你把什么放到数组里,索引总是从零(0)开始自动累加。你可以手动给索引指定一个特定的入口,等一下我会说到这点。现在我将给你演示怎么通过一个循环自动的显示一个数组的内容:

 

<?php
    $arrayPeople = array("John", "Susie", "Dave");
    $nArraySize = count($arrayPeople);
    for($index=0; $index < $nArraySize; $index++) // max. index is always number of entries - 1
                                                  // because index starts at zero
    {
        print $arrayPeople[$index];
    }
?>

在这种情况下,$index是条目的索引(地址),$nArraySize是数组元素的个数。count()函数返回数组元素的个数。对像我刚才使用的小数组而已,使用循环的确增加了代码的长度,但是当你开始处理元素成百上千的数组时(他们的确存在),你就会很乐意使用循环了。

下边我将讲讲如何创建为数组创建你自己的索引。每当我使用SESSIONS来为我的网站设定管理员权限的时候,我都会使用数组来保存seesion信息。这里是相关的代码:

<?php
    $SESSION= array(); // that creates a blank array
    $SESSION["username"] = $sUserName;
    $SESSION["password"] = $sPassword;
    $SESSION["accesslevel"] = $nLevel;
    // etc,etc,etc.
?>

看见我怎么使用单词来表示索引了吗?这样我就可以知道$SESSION["username"]包含的是人名。这比要从$SESSION[0]记起它保存的是用户名容易多了。我使用数组时总是像这样使用变量的名字代替索引来表示元素。所以为了在数组$arrayDays中保存$nDaysinMay,我会使用$arrayDays["nDaysinMay"]。这样我就能始终知道元素里包含的是什么变量。

下边我们进入多维数组。多维数组,听起来好像来自五十年代的科幻小说。其实,多维数组只是数组里的数组而已。还记得前边我怎么把变量放进数组的吗?现在我要往数组里放数组了。比如,我运营着一个网上书店,销售小说、儿童书籍和杂志,我把这些名字放到数组中。写成代码便是这样的:


<?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的数组里。这里是如何用我刚才讲到的来实现它:


<?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
?>

现在我可以用for循环来显示它们(包含上边的代码):


<?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";
            }
        }
    }
?>

现在你已经做到了。你可以在你的脚本程式中任何需要的地方创建、填充和使用数组了。如果你有关于数组和PHP方面的问题,可以按上边的邮件地址给我写信。谢谢你阅读本文。

 

本文地址:http://com.8s8s.com/it/it29227.htm