PHP Multidimensional Arrays
markdown
Multidimensional arrays are powerful array structures in PHP that allow you to store arrays inside another array. These are useful when you need to represent complex, structured data such as student records, product lists, or database rows.
In this tutorial, you will learn:
What multidimensional arrays are
Types of multidimensional arrays
How to create and access them
How to loop through nested arrays
Real-world developer examples
A multidimensional array is an array that contains one or more arrays inside it.
Example:
$students = [
["Amit", 20, "PHP"],
["Sneha", 22, "Java"],
["Raj", 21, "Python"]
];
This is a 2-dimensional array (array of arrays).
Each inner array contains data for one student.
A simple array of indexed arrays.
$marks = [
[85, 78, 92],
[88, 90, 76],
[79, 82, 84]
];
An array containing associative arrays.
$user = [
[
"name" => "Amit",
"email" => "[email protected]",
"city" => "Delhi"
],
[
"name" => "Priya",
"email" => "[email protected]",
"city" => "Mumbai"
]
];
An array that contains both indexed and associative arrays.
$products = [
"electronics" => [
["Laptop", 55000],
["Mouse", 799]
],
"clothing" => [
["Shirt", 999],
["Jeans", 1499]
]
];
You can use multiple indexes to access values.
$students = [
["Amit", 20, "PHP"],
["Sneha", 22, "Java"]
];
echo $students[0][0]; // Amit
echo $students[1][2]; // Java
foreach Loops
$students = [
["Amit", 20, "PHP"],
["Sneha", 22, "Java"]
];
foreach ($students as $student) {
foreach ($student as $value) {
echo $value . " ";
}
echo "<br>";
}
for Loops
$marks = [
[85, 78, 92],
[88, 90, 76]
];
for ($i = 0; $i < count($marks); $i++) {
for ($j = 0; $j < count($marks[$i]); $j++) {
echo $marks[$i][$j] . " ";
}
echo "<br>";
}
$employees = [
[
"id" => 101,
"name" => "Rahul",
"department" => "IT",
"salary" => 45000
],
[
"id" => 102,
"name" => "Priya",
"department" => "HR",
"salary" => 40000
],
[
"id" => 103,
"name" => "Karan",
"department" => "Finance",
"salary" => 50000
]
];
foreach ($employees as $emp) {
echo "ID: " . $emp["id"] . " - Name: " . $emp["name"] . " - Department: " . $emp["department"] . "<br>";
}
array_column() – Get a column of values
echo implode(", ", array_column($employees, "name"));
in_array() – Search inside nested arrays
if (in_array("IT", array_column($employees, "department"))) {
echo "IT Department Found";
}
json_encode() – Convert multidimensional array to JSON
echo json_encode($employees);
Use them when you need to store:
Database-like records
Product catalogues
Student lists with details
API data
Complex structured information
Multidimensional arrays are a crucial part of PHP development. They allow you to manage large, structured data sets easily. Whether you're handling database results or building API responses, mastering them helps write cleaner and more efficient code.
Learn PHP multidimensional arrays with examples. Understand how to create, access, and loop through nested arrays in PHP.
php multidimensional arrays, php nested arrays, php array examples, php tutorial for beginners
"PHP multidimensional arrays explained with examples"