Accessing 2D Array Elements
Part of the Logic & Flow section of Coddy's PHP journey — lesson 28 of 68.
Now that you know how to create 2D arrays, the next step is learning how to retrieve specific values from them. Accessing elements in a 2D array requires two indices: the first for the row and the second for the column, using the syntax $array[row][column].
Here's how you access a specific element in a 2D array:
<?php
$products = [
["Laptop", 999.99],
["Mouse", 25.50],
["Keyboard", 75.00]
];
// Access the price of the first product (Laptop)
echo $products[0][1]; // Outputs: 999.99
// Access the name of the second product (Mouse)
echo $products[1][0]; // Outputs: Mouse
?>The first index [0] selects the first row (the laptop array), and the second index [1] selects the second element within that row (the price). Think of it as navigating to a specific cell in a spreadsheet by specifying both the row and column.
This double-indexing system allows you to pinpoint exactly which piece of data you want from your grid-like structure, making 2D arrays perfect for organizing related information in rows and columns.
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: a 2D array of student information in JSON format, a row index, and a column index. Read all three inputs, convert the JSON string to a 2D array, access the element at the specified row and column indices, and print that element.
The 2D array represents students where each inner array contains: [student_name, age, grade]
Input format:
- First line: A JSON array containing student information (example:
[["Alice",16,85],["Bob",17,92],["Charlie",16,78]]) - Second line: Row index (integer)
- Third line: Column index (integer)
Expected output: The element at the specified row and column position
Cheat sheet
To access elements in a 2D array, use double-indexing with the syntax $array[row][column]:
<?php
$products = [
["Laptop", 999.99],
["Mouse", 25.50],
["Keyboard", 75.00]
];
// Access the price of the first product (Laptop)
echo $products[0][1]; // Outputs: 999.99
// Access the name of the second product (Mouse)
echo $products[1][0]; // Outputs: Mouse
?>The first index selects the row, and the second index selects the column within that row. This allows you to pinpoint exactly which piece of data you want from your grid-like structure.
Try it yourself
<?php
// Read the JSON array
$jsonInput = trim(fgets(STDIN));
$students = (array)json_decode($jsonInput, true);
// Read the row index
$rowIndex = intval(fgets(STDIN));
// Read the column index
$colIndex = intval(fgets(STDIN));
// TODO: Write your code below to access the element at the specified row and column
// Output the result
echo $element;
?>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