• 升级为终身会员只需57元,即可任意下载本站所有教程!
  • 每天签到,可以赚取金币!
  • 赞积分可以兑换金币、兑换实物礼品!

6.3、定义PHP数组(简单数组、多维数组)

  • 高清线路
  • 备用线路
  • 图文解说

定义简单数组

<?php
$array = array("a", "b", "c");
print_r($array);
?>
<?php
$array = array("a", "b", "c");
echo $array[ 1 ];
?>
<?php
$array = array(
    "a" => "first",
    "b" => "second",
);
print_r($array);
echo "<br>";
echo $array["a"];
echo $array["b"];
?>
<?php
$array[1] = 1;
$array[2] = 2;
var_dump($array);
?>

定义多维数组

<?php
$products = array(array( 'TIR', 'Tires', 100),
               array( 'OIL', 'oil', 10),
               array( 'SPK', 'Spark Plugs', 4));
?>
<?php
$products = array(array( 'TIR', 'Tires', 100),
               array( 'OIL', 'oil', 10),
               array( 'SPK', 'Spark Plugs', 4));

echo '|'.$products[0][0].'|'.$products[0][1].'|'.$products[0][2].'|<br />';
echo '|'.$products[1][0].'|'.$products[1][1].'|'.$products[1][2].'|<br />';
echo '|'.$products[2][0].'|'.$products[2][1].'|'.$products[2][2].'|<br />';
?> 
<?php
$products = array(array( 'TIR', 'Tires', 100),
               array( 'OIL', 'oil', 10),
               array( 'SPK', 'Spark Plugs', 4));
for($row=0 ;$row<3;$row++){
    for($colum=0; $colum<3; $colum++){
        echo '|'.$products[$row][$colum];    
    }
    echo '|<br />';
}
?> 
<?php
$products = array(array('Code' => 'TIR',
                        'Description' => 'Tires', 
                        'Price' => 100),
                array('Code' => 'OIL', 
                        'Description' => 'oil', 
                        'Price' =>10),
                array('Code' => 'SPK', 
                        'Description' => 'Spark Plugs',
                        'Price' =>4));
?> 
<?php
$products = array(array('Code' => 'TIR',
                        'Description' => 'Tires', 
                        'Price' => 100),
                array('Code' => 'OIL', 
                        'Description' => 'oil', 
                        'Price' =>10),
                array('Code' => 'SPK', 
                        'Description' => 'Spark Plugs',
                        'Price' =>4));
for ( $row = 0; $row < 3; $row++ ){ 
    echo '|'.$products[$row]['Code'].
        '|'.$products[$row]['Description'].
        '|'.$products[$row]['Price'].'|<BR>';
}
?> 
<?php
$products = array(array('Code' => 'TIR',
                        'Description' => 'Tires', 
                        'Price' => 100),
                array('Code' => 'OIL', 
                        'Description' => 'oil', 
                        'Price' =>10),
                array('Code' => 'SPK', 
                        'Description' => 'Spark Plugs',
                        'Price' =>4));

for ( $row = 0; $row < 3; $row++ ){
    while ( list( $key, $value ) = each( $products[ $row ] ) ){
        echo "|$value";
    }
    echo "|<BR>";
}
?> 
<?php
$categories = array( array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 ),
                 array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 ),
                 array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 )
           );
?> 
<?php
$categories = array( array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 ),
                 array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 ),
                 array ( array( "TIR", "Tires", 100 ),
                      array( "OIL", "Oil", 10 ),
                      array( "SPK", "Spark Plugs", 4 )
                 )
           );
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>";
    }
}
?> 
63.1K
关于我们 | 联系我们 | 使用协议 | 版权声明 | 网站地图 | 广告服务 | 友情链接| RSS订阅|
粤ICP备15104870号-5

机会是留给有准备的人的,舒服是留给死去的人的!
社会是在不断地变化发展的,要使自己的思想适应新的变化,那就得学习,不学习就淘汰。 —— 毛泽东
声明:本站部份原创外均收集于网络闲散资源以及网友上传提供,收费下载只为服务器资费所需和防止别人恶意下载并高价盗卖等!
收集宗旨是为了不让好资源浪费,作者辛苦录制的视频无人知悉岂不可惜!同时也帮助一些无法支付超贵培训费的学员自学!如无意中侵犯了您的权益请联系删除!Thank !
'); })();