Change language

Multidimensional associative array in PHP

|
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 following program demonstrates how to create a multidimensional associative array: $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 ​​ );  
?>
Exit:
 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 the above program, the parent index is Python and PHP. The parent key is associated with an array of constant value keysets. The last key, that is, the description of each parent key, was associated with another array of a set of keys and constant values. Here Python and PHP are the parent key for first_release, latest_release, design_by and description, while description is the parent key for extension, typing_discipline and license.Extracting values:we can get the value multidimensional array using the following method:
  • Key Usage:We can use the associative array key to get the data value directly. Example:   $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’ ];  
    ?>
    Exit:
     Array ([ extension] = > .py [typing_discipline] = > Duck, dynamic, gradual [license] = > Python Software Foundation License) 3.8.0 
  • Using a foreach loop:we can use a foreach loop to retrieve the value of each key associated within a multidimensional associative array. 
    Example : $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 . "" ; foreach ( $value as $sub_key = > $sub_val ) { // if sub_val is an array, then again

    // iterate over each of its elements // otherwise just print the sub_key value // and sub_val if ( is_array ( $sub_v al )) { echo $sub_key . ":" ; foreach ( $sub_val as $k = > $v ) { echo "" . $k . "=" . $v . "" ; } } else { echo $sub_key . "=" . $sub_val . "" ; } } }  
    ?>
    Exit:

     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 
  • Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method