Tags
Asked 3 years ago
9 Jun 2021
Views 308
kiran

kiran posted

PHP Multidimensional associative arrays

how to handle PHP Multidimensional associative arrays ?
sec8

sec8
answered Apr 26 '23 00:00

In PHP, a multidimensional associative array is an array that contains one or more arrays, where each array has a set of key-value pairs. These arrays are also called associative arrays because they use string keys instead of numeric indexes to access the values.

To create a multidimensional associative array in PHP, you can use the following syntax:



$array = array(
    array(
        "name" => "John",
        "age" => 30,
        "city" => "New York"
    ),
    array(
        "name" => "Mary",
        "age" => 25,
        "city" => "Los Angeles"
    ),
    array(
        "name" => "Peter",
        "age" => 40,
        "city" => "Chicago"
    )
);

In this example, we have an array that contains three arrays, and each of those arrays has three key-value pairs: name, age, and city. To access a specific value in the multidimensional array, you need to specify the index of the outer array and the key of the inner array. For example, to access the age of Mary, you can use the following syntax:



echo $array[1]["age"]; // Output: 25
Post Answer