PHP中如何使用多维关联数组?代码实例

2021年3月19日18:12:45 发表评论 981 次浏览

PHP多维数组用于存储与常量值相反的数组。关联数组以键和值对的形式存储数据, 其中键可以是整数或字符串。多维关联数组通常用于以组关系存储数据。

创建:

我们可以通过将包含一组键和值对的数组映射到父键来创建多维关联数组。

下面的程序演示了如何创建多维关联数组:

<?php
  
$languages = array ();
  
$languages [ 'Python' ] = array (
     "first_release" => "1991" , "latest_release" => "3.8.0" , "designed_by" => "Guido van Rossum" , "description" => array (
         "extension" => ".py" , "typing_discipline" => "Duck, dynamic, gradual" , "license" => "Python Software Foundation License"
     )
);
  
$languages [ 'PHP' ] = array (
     "first_release" => "1995" , "latest_release" => "7.3.11" , "designed_by" => "Rasmus Lerdorf" , "description" => array (
         "extension" => ".php" , "typing_discipline" => "Dynamic, weak" , "license" => "PHP License (most of Zend engine
              under Zend Engine License)"
     )
);
  
print_r( $languages );
  
?>

输出如下:

Array
(
    [Python] => Array
        (
            [first_release] => 1991
            [latest_release] => 3.8.0
            [designed_by] => Guido van Rossum
            [description] => Array
                (
                    [extension] => .py
                    [typing_discipline] => Duck, dynamic, gradual
                    [license] => Python Software Foundation License
                )

        )

    [PHP] => Array
        (
            [first_release] => 1995
            [latest_release] => 7.3.11
            [designed_by] => Rasmus Lerdorf
            [description] => Array
                (
                    [extension] => .php
                    [typing_discipline] => Dynamic, weak
                    [license] => PHP License (most of Zend engine
             under Zend Engine License)
                )

        )

)

说明:在上面的程序中, 父索引是Python和PHP。父键与一组具有恒定值的键集相关联。最后一个键, 即每个父键的描述已与该组键和常量值的另一个数组相关联。在这里, Python和PHP是first_release, latest_release, designed_by和description的父键, 而description是扩展名, typeing_discipline和许可证的父键。

检索值:我们可以使用以下方法检索多维数组的值:

使用键:

我们可以使用关联数组的键直接检索数据值。

例子:

<?php
  
$languages = array ();
  
$languages [ 'Python' ] = array (
     "first_release" => "1991" , "latest_release" => "3.8.0" , "designed_by" => "Guido van Rossum" , "description" => array (
         "extension" => ".py" , "typing_discipline" => "Duck, dynamic, gradual" , "license" => "Python Software Foundation License"
     )
);
  
print_r( $languages [ 'Python' ][ 'description' ]);
echo $languages [ 'Python' ][ 'latest_release' ];
  
?>

输出如下:

Array
(
    [extension] => .py
    [typing_discipline] => Duck, dynamic, gradual
    [license] => Python Software Foundation License
)
3.8.0

使用foreach循环:

我们可以使用foreach循环来检索多维关联数组中关联的每个键的值。

例子:

<?php
  
$languages = array ();
  
$languages [ 'Python' ] = array (
     "first_release" => "1991" , "latest_release" => "3.8.0" , "designed_by" => "Guido van Rossum" , "description" => array (
         "extension" => ".py" , "typing_discipline" => "Duck, dynamic, gradual" , "license" => "Python Software Foundation License"
     )
);
  
foreach ( $languages as $key => $value ) {
     echo $key . "\n" ;
     foreach ( $value as $sub_key => $sub_val ) {
                  
         // If sub_val is an array then again
         // iterate through each element of it
         // else simply print the value of sub_key
         // and sub_val
         if ( is_array ( $sub_val )) {
             echo $sub_key . " : \n" ;
             foreach ( $sub_val as $k => $v ) {
                 echo "\t" . $k . " = " . $v . "\n" ;
             }
         } else {
             echo $sub_key . " = " . $sub_val . "\n" ;
         }
     }
}
  
?>

输出如下:

Python
first_release = 1991
latest_release = 3.8.0
designed_by = Guido van Rossum
description : 
    extension = .py
    typing_discipline = Duck, dynamic, gradual
    license = Python Software Foundation License

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: