Multidimensional Associative Array in PHP

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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
    


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.