码迷,mamicode.com
首页 > 编程语言 > 详细

第三章 使用数组(2)

时间:2016-07-21 23:42:08      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

 

  • 多维数组

    二维数组:相当于1个矩阵,或者一个具有宽度和高度 或者行和列 的网格。例如:技术分享这个图用一个二维数组保存Bob的产品,每一行代表一种产品(product),

         每一列代表产品的属性(product attribute)。

         PHP代码实现:

//create a two-dimensional array
$products2 = array(array(‘TIR‘,‘Tires‘,100),
                    array(‘OIL‘,‘Oil‘,10),
                    array(‘SPK‘,‘Spark Plugs‘,4));
//show this array in the browser
echo ‘|‘.$products2[0][0].‘|‘.$products2[0][1].‘|‘.$products2[0][2]."<br />";
echo ‘|‘.$products2[1][0].‘|‘.$products2[1][1].‘|‘.$products2[1][2]."<br />";
echo ‘|‘.$products2[2][0].‘|‘.$products2[2][1].‘|‘.$products2[2][2]."<br />";

 

          示例效果:技术分享

          还可以用双重for循环来实现同样的效果:

for($row = 0;$row < 3;$row++){
    for($column = 0;$column < 3;$column++){
        echo "|".$products2[$row][$column];
    }
    echo "<br />";
}

          另一种PHP代码实现(创建列名称来代替数字)-(描述型索引):

//create a two-dimensional array
$products3 = array(array(‘Code‘=>‘TIR‘,‘Description‘=>‘Tires‘,‘Price‘=>100),
                    array(‘Code‘=>‘OIL‘,‘Description‘=>‘Oil‘,‘Price‘=>10),
                    array(‘Code‘=>‘SPK‘,‘Description‘=>‘Spark Plugs‘,‘Price‘=>4));
//show this array in the browser
for($row = 0;$row < 3;$row++){
    echo "|".$products3[$row][‘Code‘]."|".$products3[$row][‘Description‘]."|".$products3[$row][‘Price‘]."<br />";
}

 

          示例显示效果与前面的示例显示效果一模一样。

          还可以在while循环中使用each()和list()函数来遍历整个内部数组:

//show this array in the browser
for($row = 0;$row < 3;$row++){
    while(list($key,$value)=each($products3[$row])){
        echo "|$value";
    }
    echo "<br />";
}

如果Bob想对他的商品进行分类:CAR,Van , Truck,则使用三维数组:

//create a three-dimensional array
$categories = array(array(array(‘CAR_TIR‘,‘Tires‘,100),
                            array(‘CAR_OIL‘,‘OIl‘,10),
                            array(‘CAR_SPK‘,‘Spark Plugs‘,4)
                            ),
                    array(array(‘VAN_TIR‘,‘Tires‘,100),
                            array(‘VAN_OIL‘,‘Oil‘,10),
                            array(‘VAN_SPK‘,‘Spark Plugs‘,4)
                            ),
                    array(array(‘TRK_TIR‘,‘Tires‘,100),
                            array(‘TRK_OIL‘,‘Oil‘,10),
                            array(‘TRK_SPK‘,‘Spark Plugs‘,4)
                        )
                    );
//show this array in the browser
for($layer = 0;$layer < 3;$layer++){
    echo "Layer $layer<br />";
    for($row = 0;$row < 3;$row++){
        for($column = 0;$column < 3;$column++){
            echo "|".$categories[$layer][$row][$column];
        }
        echo "<br />";
    }
}

以此类推,可以创建四维、五维或六维数组等。

  • 数组排序

    使用sort()函数:

$products4 = array(‘Tires‘,‘Oil‘,‘Spark Plugs‘);
sort($products4);

    现在产品顺序变为:Oil、Spark Plugs、Tires

    注意:sort()函数区分大小写,且所有大写字母都在小写字母前面,例如:

$products4 = array(‘Tires‘,‘oil‘,‘Spark Plugs‘);
sort($products4);
for($row = 0;$row < 3;$row++){
    echo $products4[$row]." ";
}

    显示结果为:技术分享

    使用asort()函数和ksort()函数对关联数组进行排序:如果用关联数组存储各个项目和它们的价格,就需要使用不同的排序函数使关键字和值在排序时仍然保持一致。

   下面的代码将创建一个包含3个产品及价格的数组,然后将它们按照价格的升序进行排序:

$products5 = array(‘Tires‘=>100,‘Oil‘=>10,‘Spark Plugs‘=>4);
asort($products5);
foreach ($products5 as $key => $val){
    echo "$key = $val<br />";
}

    注意asort要用foreach来遍历,结果为:技术分享

    使用ksort来排序:

$products6 = array(‘Tires‘=>100,‘Oil‘=>10,‘Spark Plugs‘=>4);
ksort($products6);
foreach ($products6 as $key => $val){
    echo "$key = $val<br />";
}

    示例结果为:技术分享

    除此之外,sort()/asort()/ksort()涵涵素各对应一个反向排序的函数:rsort(),arsort(),krsort() .

 

第三章 使用数组(2)

标签:

原文地址:http://www.cnblogs.com/yojiaku/p/5693375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!