A multidimensional associative array is an array where each element is an associative array itself. This allows you to store complex data structures such as records with multiple properties, employee records, products in a store, or student data.
The basic structure of a multidimensional associative array:
// Defining a multidimensional associative array
$array = array(
"key1" => array("subkey1" => "value1", "subkey2" => "value2"),
"key2" => array("subkey1" => "value3", "subkey2" => "value4")
);
This example demonstrates how to create and access a multidimensional associative array:
// Creating employee records using a multidimensional associative array
<?php
$employees = array(
"Peter" => array("age" => 35, "position" => "Manager", "department" => "HR"),
"John" => array("age" => 28, "position" => "Developer", "department" => "IT"),
"Doe" => array("age" => 40, "position" => "Designer", "department" => "Marketing")
);
echo $employees["Peter"]["position"];
?>
The output will be Manager. The first key selects the employee name, and the second key accesses a specific property of that employee.
You can visualize this structure as a table-like hierarchy where each employee name maps to multiple attributes such as age, position, and department.
Search for an employee to see how the multidimensional keys are linked.
| Main Key (Name) | Subkey: Age | Subkey: Position | Subkey: Department |
|---|
isset() or array_key_exists()foreach