Multidimensional arrays php
In PHP programming, a multi-dimensional array is a collection of tabular arrays, grouped into rows and columns, containing one or more arrays within an array. Multi-dimensional arrays in PHP can be displayed in either an indexed or associative array format. These are particularly useful for PHP programs displaying or processing more complex data array element structures, such as multi-dimensional arrays, matrices, grids, or tables.

Multidimensional indexed arrays in PHP.
In PHP programming, a multi-dimensional indexed array is an array data type element where each element in a multi-dimensional array is automatically an array, and the elements of the arrays within these arrays are stored in a row-column format, sequenced in numeric order.
Example of a multidimensional indexed array in PHP.
<?php
$table = [
[9, 1, 4],
[4, 3, 3],
[10, 12, 14],
[19, 22, 33]
];
echo $table [0][0]; // Result – 9
echo “\n”;
echo $table [1][2]; // Result – 3
echo “\n”;
echo $table [2][1]; // Result – 12
echo “\n”;
echo $table [3][2]; // Result – 33
?>
Multidimensional indexed array explanation.
- Here $table [0] is an array [1, 2, 3], and $table [3] [2] accesses and displays array element 33.
Traversing Multidimensional indexed arrays in PHP.
In PHP programs, programmers can apply nested foreach loops or for loops to store multi-dimensional indexes in an array sequence and perform a start-to-end loop.
<?php
$table = [
[99, 100, 300],
[700, 300, 600],
[200, 400, 900]
];
foreach ($table as $rows) {
foreach ($rows as $values) {
echo $values . ” “;
}
echo “\n”;
}
?>
Multidimensional associative arrays in PHP.
A multi-dimensional associative array in PHP is an array data type element structure in which each array element is another array data type element, and these internal arrays use custom array key values instead of numeric indexes.
Example of a multidimensional associative array in PHP.
<?php
$employee = [
“Siddhi” => [“contact” => “111-222-333”, “email” => “siddhi@domain.com”],
“Harry” => [“contact” => “444-555-666”, “email” => “harry@domain.com”],
“Lalita” => [“contact” => “777-888-999”, “email” => “lalita@domain.com”]
];
echo $employee[“Siddhi”][“contact”]; // Result – 111-222-333
echo “\n”;
echo $employee[“Lalita”][“email”]; // Result – lalita@domain.com
?>
In the multidimensional associative array example,
- $contacts[“Siddhi”][“contact”] accesses and displays the array element “111-222-333” from the associative array.
- In this, each employee, for example, “siddhi,” has an array associated with the key values ”contact” and “email.”
Traveling multidimensional associative arrays in PHP.
In a multidimensional PHP program, programmers can access and display array elements by looping through multidimensional associative arrays by applying nested foreach loops.
<?php
$employee = [
“Rock” => [“contact” => “111-222-333”, “email” => “rock@domain.com”],
“John” => [“contact” => “444-555-666”, “email” => “john@domain.com”],
“Harry” => [“contact” => “777-888-999”, “email” => “harry@domain.com”]
];
foreach ($employee as $emp_name => $emp_info) {
echo “$emp_name employee info \n”;
foreach ($emp_info as $key => $values) {
echo “$key: $values \n”;
}
echo “\n”;
}
?>
Multidimensional arrays with mixed types in PHP.
In PHP, programmers can create multidimensional arrays by combining both indexed and associated array types, providing greater flexibility in array processing and display.
Example of a mixed multidimensional array in PHP.
<?php
$employee = [
[“emp_name” => “Harry”, “emp_age” => 21, “address” => “Delhi”],
[“emp_name” => “Siddhi”, “emp_age” => 27, “address” => “Mumbai”],
[“emp_name” => “David”, “emp_age” => 31, “address” => “Chennai”]
];
echo $employee[1][“emp_name”]; // Result – Siddhi
echo “\n”;
echo $employee[1][“emp_age”]; // Result – 27
echo “\n”;
echo $employee[2][“address”]; // Result – Chennai
?>
In the mixed multidimensional array example.
- Here, each employee is represented by an associated array containing their basic information.
- Where the main array $employee is an indexed array data type that stores associated array data type elements.
Adding an element to a multidimensional array in PHP.
In a PHP program, programmers can add array elements to a multidimensional array in a dynamic order by specifying the array keys and index locations.
Example of adding an element to a multidimensional array.
<?php
$employee = [
“Siddhi” => [“contact” => “111-222-333”, “email” => “siddhi@domain.com”]
];
// Here we are adding a new employee row to the contacts.
$employee[“Vinay”] = [“contact” => “444-555-666”, “email” => “vinay@domain.com”];
$employee[“Amit”] = [“contact” => “777-888-999”, “email” => “amit@domain.com”];
$employee[“Lalit”] = [“contact” => “101-102-103”, “email” => “lalit@yahoo.com”];
// here we are adding a new location address field for Kunal
$employee[“Kunal”][“location”] = “abc street block – 14 New Delhi.”;
print_r($employee);
?>
In the example of adding an element to a multidimensional array,
- Here we have added new employees Vinay, Amit, and Lalit to the $employee array.
- Here we are also adding a new field, location, for the employee “Kunal”.
Accessing multidimensional array elements using foreach in PHP.
In a multi-dimensional array in PHP, programmers can also access multi-dimensional array elements in a dynamic order with a foreach loop. Here’s a better dynamic method for printing each multi-dimensional array key-value pair.
<?php
$employee = [
[“emp_name” => “harry”, “emp_age” => 27, “address” => “delhi”],
[“emp_name” => “siddhi”, “emp_age” => 19, “address” => “mumbai”],
[“emp_name” => “bhavishi”, “emp_age” => 23, “address” => “kolkata”]
];
foreach ($employee as $info) {
foreach ($info as $key => $values) {
echo “$key – $values \n”;
}
echo “\n”;
}
?>
A summary of multi-dimensional arrays in PHP programming.
- In PHP programming, a multi-dimensional array is a data type collection of other arrays, either indexed or associatively combined.
- In PHP programming, programmers can create flexible array structures by merging indexed and associative arrays.
- In PHP programming, access array elements by referencing multi-dimensional array indexes or keys, and display array elements from start to end by applying a foreach loop to nested arrays.
- In PHP programming, you can add new elements to multi-dimensional arrays in a dynamic order by providing a reference to specific indexes/keys in the multi-dimensional array.
- Multi-dimensional arrays are more helpful in creating and displaying more complex data structures such as databases, matrices, or tables.

