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:
<?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)
)
)
)
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:
- 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'];?>chevron_rightfilter_noneOutput:Array ( [extension] => .py [typing_discipline] => Duck, dynamic, gradual [license] => Python Software Foundation License ) 3.8.0 - 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($languagesas$key=>$value) {echo$key."\n";foreach($valueas$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_valif(is_array($sub_val)) {echo$sub_key." : \n";foreach($sub_valas$k=>$v) {echo"\t".$k." = ".$v."\n";}}else{echo$sub_key." = ".$sub_val."\n";}}}?>chevron_rightfilter_noneOutput: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
Recommended Posts:
- How to loop through an associative array and get the key in PHP?
- How to remove a key and its value from an associative array in PHP ?
- How to check an array is associative or sequential in PHP?
- Convert an object to associative array in PHP
- How to get numeric index of associative array in PHP?
- How to get a list of associative array keys in JavaScript ?
- Iterate associative array using foreach loop in PHP
- How to access an associative array by integer index in PHP?
- How to remove Objects from Associative Array in JavaScript ?
- PHP program to add item at the beginning of associative array
- PHP multidimensional array search by value
- How to check an array is multidimensional or not in PHP ?
- How to search by key=>value in a multidimensional array in PHP ?
- Multidimensional array in JavaScript
- Convert multidimensional array to XML file in PHP
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.


