Open In App

Multidimensional Associative Array in PHP

Last Updated : 31 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.

Creation: We can create a multidimensional associative array by mapping an array containing a set of key and value pairs to the parent key.
The below program demonstrate how to create a multidimensional associative array:




<?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);
  
?>


Output:

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)
                )

        )

)

Explanation: In above program, parent index are Python and PHP. The parent key is associated with an array of sets of keys with constant values. The last key i.e. description of each parent key has been associated with another array of the set of keys and constant values. Here Python and PHP are parent key for first_release, latest_release, designed_by and description whereas description is parent key for the extension, typing_discipline, and license.

Retrieving Values: We can retrieve the value of multidimensional array using the following method:

  1. Using key: We can use key of the associative array to directly retrieve the data value.

    Example:




    <?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'];
      
    ?>

    
    

    Output:

    Array
    (
        [extension] => .py
        [typing_discipline] => Duck, dynamic, gradual
        [license] => Python Software Foundation License
    )
    3.8.0
    
  2. Using foreach loop: We can use foreach loop to retrieve value of each key associated inside the multidimensional associative array.
    Example:




    <?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";
            }
        }
    }
      
    ?>

    
    

    Output:

    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 is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: