Creating a 2D Array
Part of the Logic & Flow section of Coddy's PHP journey — lesson 27 of 68.
A multi-dimensional array is simply an array that contains other arrays as its elements. The most common type is a 2D array, which you can think of as a grid with rows and columns, similar to a spreadsheet or a game board.
Creating a 2D array in PHP is straightforward—you build an array where each element is itself an array:
<?php
$gameBoard = [
["", "", ""],
["", "", ""],
["", "", ""]
];
?>This creates a 3x3 grid where each inner array represents a row, and each element within those arrays represents a column. You can also create 2D arrays with different data types:
<?php
$scores = [
[85, 92, 78],
[90, 88, 95],
[76, 84, 89]
];
?>PHP also provides the built-in array_fill() function to quickly create an array filled with a given value. It takes three arguments: the starting index, the number of elements, and the fill value:
<?php
$row = array_fill(0, 3, "");
// Result: ["", "", ""]
?>This is a handy shortcut when you need to initialize arrays with repeated values, such as empty cells in a game board.
Multi-dimensional arrays are incredibly useful for organizing structured data like game boards, tables of information, or any scenario where you need to represent data in a grid format. They provide a natural way to work with rows and columns of related information.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive three inputs representing the dimensions and initial value for a 2D array: the number of rows, the number of columns, and a value to fill each position. Read these inputs, create a 2D array with the specified dimensions where each element is set to the given value, and print the array using print_r().
Input format:
- First line: Number of rows (integer)
- Second line: Number of columns (integer)
- Third line: Initial value for each element (string)
Expected output: The created 2D array displayed using print_r()
Cheat sheet
A multi-dimensional array is an array that contains other arrays as elements. The most common type is a 2D array, which represents a grid with rows and columns.
Creating a 2D array in PHP:
<?php
$gameBoard = [
["", "", ""],
["", "", ""],
["", "", ""]
];
?>2D arrays with different data types:
<?php
$scores = [
[85, 92, 78],
[90, 88, 95],
[76, 84, 89]
];
?>Each inner array represents a row, and each element within those arrays represents a column. Multi-dimensional arrays are useful for organizing structured data like game boards, tables, or any grid-format data.
Use array_fill() to create an array pre-filled with a given value:
array_fill(start_index, count, value)
<?php
// Creates [0, 0, 0, 0, 0]
$row = array_fill(0, 5, 0);
?>This is useful for quickly initializing rows or entire 2D arrays with a default value.
Try it yourself
<?php
// Read input
$rows = intval(fgets(STDIN));
$columns = intval(fgets(STDIN));
$value = trim(fgets(STDIN));
// TODO: Write your code below to create the 2D array
// Output the result
print_r($array);
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting