教程分类

Product category

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

编辑:57自学网 发布日期:2019-11-29 17:20
  • 高清线路
  • 备用线路
  • 图文解说

定义简单数组

<?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>";
    }
}
?> 
上一篇:6.2、数组类型(索引数组、关联数组) 下一篇:6.4、使用数组(输出数组、统计元素个数、遍历数组、转换数组、数组排序、数组指针)