📜  PHP的多维关联数组

📅  最后修改于: 2022-05-13 01:54:11.094000             🧑  作者: Mango

PHP的多维关联数组

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

创建:我们可以通过将包含一组键值对的数组映射到父键来创建多维关联数组。
下面的程序演示了如何创建一个多维关联数组:

 "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 是扩展、typing_discipline 和 license 的父密钥。

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



  1. 使用key:我们可以使用关联数组的key直接获取数据值。

    例子:

     "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
    
  2. 使用 foreach 循环:我们可以使用 foreach 循环来检索多维关联数组内关联的每个键的值。
    例子:
     "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
    

PHP是一种专门为 Web 开发设计的服务器端脚本语言。您可以按照此PHP教程和PHP示例从头开始学习PHP 。