Open In App

Multidimensional Associative Array in PHP

Last Updated : 31 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Previous Article
Next Article

Similar Reads

Convert an object to associative array in PHP
An object is an instance of a class. It is simply a specimen of a class and has memory allocated. Array is the data structure that stores one or more similar type of values in a single name but associative array is different from a simple PHP array. An array which contains string index is called associative array. It stores element values in associ
2 min read
Iterate associative array using foreach loop in PHP
Given two arrays arr1 and arr2 of size n. The task is to iterate both arrays in the foreach loop. Both arrays can combine into a single array using a foreach loop. Array: Arrays in PHP is a type of data structure that allows to storing multiple elements of similar data type under a single variable thereby saving the effort of creating a different v
2 min read
How to check an array is associative or sequential in PHP?
In PHP there is no need to write the variable type before the variable because it is loosely-typed. It takes datatype from user defined values that are stored in it. Arrays in PHP is a type of data structure that allows to store multiple elements of similar data type under a single variable thereby saving the effort of creating a different variable
2 min read
How to access an associative array by integer index in PHP?
There are two types of arrays in PHP, indexed and associative arrays. In case of indexed array strict numeric indexing is followed but in case of associative array there are keys corresponding to each element. The elements of an associative array can only be accessed by the corresponding keys. As there is not strict indexing between the keys, acces
3 min read
PHP program to add item at the beginning of associative array
In PHP associative array is the type of array where the index need not to be strictly sequential like indexed array. Normally add a new element in an existing associative array it will get appended at the end of that array. Example: &lt;?php // Existing array $arr = array('one' =&gt; 1, 'two' =&gt; 2); // New element $arr['zero'] = 0; // Final arra
3 min read
How to get numeric index of associative array in PHP?
In PHP we can associate name/label with each array elements using =&gt; symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value. Using array_keys() function: The array_keys() function is an inbuilt function in PHP which is used to return either all the keys of a
1 min read
How to loop through an associative array and get the key in PHP?
Associative Array: Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subject’s names as the keys in our associative array, and the value would be their respective marks
2 min read
How to remove a key and its value from an associative array in PHP ?
Given an associative array containing array elements and the task is to remove a key and its value from the associative array. Examples: Input : array( "name" =&gt; "Anand", "roll"=&gt; "1") Output : Array ( [roll] =&gt; 1 ) Input : array( "1" =&gt; "Add", "2" =&gt; "Multiply", "3" =&gt; "Divide") Output : Array ( [2] =&gt; Multiply [3] =&gt; Divid
2 min read
How to sort an Array of Associative Arrays by Value of a Given Key in PHP ?
Each entry in the associative array is characterized by a unique key-value pair. An array can contain singular data types belonging to variables or other arrays as its elements. There are multiple ways to sort an array of associative arrays by the value of a specified key. Approach 1: Using the array_multisort() method The array_multisort() method
4 min read
How to sort an associative array by key in PHP?
Sorting an associative array by key is a common task in PHP, especially when you need to organize data alphabetically or numerically based on keys. PHP provides built-in functions to accomplish this task efficiently, allowing developers to rearrange the order of elements within an associative array based on their keys. Approach:Using ksort() Functi
2 min read