Two Dimensional Array in PHP
Two Dimensional array can be defined as a multidimensional array in PHP program. Two Dimensional Array represented as the collection of rows and columns.
Declaration of Two Dimensional Array:
To declare the 2D array in PHP programming language.
Syntax:-
<?php
// Declare a 2D array (e.g., 3 rows, 3 columns)
$Array_name = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
?>
Example:-
<?php
$rows = 4;
$cols = 5;
$a = [];
for ($i = 0; $i < $rows; $i++) {
$a[$i] = array_fill(0, $cols, 0); // Fill each row with 5 zeros
}
?>
Here, a is Two-dimensional array, this array can hold 6 elements.

Initialization of Two Dimensional Array:
Multidimensional arrays initialized by specifying bracketed values for every row.
<?php
$score = [
[100, 221],
[59, 54],
[10, 21]
];
?>
Example:-
<?php
$score = [
[100, 221],
[59, 54],
[10, 21]
];
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 2; $j++) {
echo "Element[$i][$j] = " . $score[$i][$j] . "\n";
}
}
?>
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com