📖 Scenario: You are managing a small library system. You want to store information about books, including their title, author, and year published. This information will be stored in a multidimensional array.
🎯 Goal: Create a multidimensional array to hold book information, add a new book, retrieve specific book details, and display the list of books.
📋 What You'll Learn
Create a multidimensional array called library with three books, each book having keys title, author, and year.
Add a new book to the library array.
Use a foreach loop to iterate over the library array and access each book's details.
Print the details of each book in a readable format.
💡 Why This Matters
🌍 Real World
Multidimensional arrays are useful to store and organize complex data like records, tables, or objects in many applications such as libraries, inventories, or user profiles.
💼 Career
Understanding multidimensional arrays is essential for backend developers, data analysts, and anyone working with structured data in PHP or similar languages.
Progress0 / 4 steps
1
Create the initial multidimensional array
Create a multidimensional array called library with these exact three books: [ 'title' => '1984', 'author' => 'George Orwell', 'year' => 1949 ], [ 'title' => 'To Kill a Mockingbird', 'author' => 'Harper Lee', 'year' => 1960 ], [ 'title' => 'The Great Gatsby', 'author' => 'F. Scott Fitzgerald', 'year' => 1925 ].
PHP
Hint
Remember to use an array of arrays, where each inner array holds keys 'title', 'author', and 'year'.
2
Add a new book to the library
Add a new book to the library array with these details: 'title' => 'Pride and Prejudice', 'author' => 'Jane Austen', 'year' => 1813.
PHP
Hint
Use the array append syntax $library[] = to add the new book.
3
Loop through the library to access book details
Use a foreach loop with variables $book to iterate over the $library array and access each book's details.
PHP
Hint
Use foreach ($library as $book) to loop through each book array.
4
Print the details of each book
Inside the foreach loop, print each book's title, author, and year in this format: Title: [title], Author: [author], Year: [year]. Use echo to display the output.
PHP
Hint
Use echo with double quotes and curly braces to insert array values inside the string.
Practice
(1/5)
1. What is a multidimensional array in PHP?
easy
A. An array containing one or more arrays inside it
B. An array with only string keys
C. An array that can only hold numbers
D. An array that cannot be changed after creation
Solution
Step 1: Understand array nesting
A multidimensional array is an array that contains other arrays as its elements.
Step 2: Identify the correct definition
An array containing one or more arrays inside it correctly describes this nesting, while others describe unrelated properties.
Final Answer:
An array containing one or more arrays inside it -> Option A
Quick Check:
Multidimensional array = nested arrays [OK]
Hint: Think of arrays inside arrays like boxes within boxes [OK]
Common Mistakes:
Confusing multidimensional arrays with simple arrays
Thinking keys must be strings only
Believing arrays cannot be changed
2. Which of the following is the correct way to access the element 'blue' in this array?
$colors = [['red', 'green'], ['blue', 'yellow']];
easy
A. $colors[1][0]
B. $colors[0][2]
C. $colors[2][1]
D. $colors[1][1]
Solution
Step 1: Identify the position of 'blue'
'blue' is in the second inner array (index 1) and is the first element (index 0) there.
Step 2: Use correct indices to access
Accessing $colors[1][0] retrieves 'blue'. Other options point to wrong indices or elements.
Final Answer:
$colors[1][0] -> Option A
Quick Check:
Second array, first element = 'blue' [OK]
Hint: Count arrays and elements starting at zero [OK]
The array starts with [[ but only one closing bracket ] is present before the semicolon.
Step 2: Identify missing bracket
The outer array requires a closing bracket ], so the code should end with ]];
Final Answer:
Missing closing bracket for the outer array -> Option B
Quick Check:
Outer array brackets must match [OK]
Hint: Count opening and closing brackets carefully [OK]
Common Mistakes:
Confusing inner and outer brackets
Ignoring syntax errors
Assuming semicolon fixes bracket issues
5. Given this multidimensional array representing students and their scores: <pre>$students = [ ['name' => 'Alice', 'scores' => [85, 90]], ['name' => 'Bob', 'scores' => [78, 82]], ['name' => 'Charlie', 'scores' => [92, 88]] ];
Which code correctly calculates and prints the average score of Bob?
hard
A. $avg = array_sum($students[2]['scores']) / count($students[2]['scores']); echo $avg;
B. $avg = array_sum($students['Bob']['scores']) / count($students['Bob']['scores']); echo $avg;
C. $avg = array_sum($students[1]['score']) / count($students[1]['score']); echo $avg;
D. $avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg;
Solution
Step 1: Locate Bob's data in the array
Bob is at index 1, and his scores are under the key 'scores'.
Step 2: Calculate average correctly
Use array_sum and count on $students[1]['scores'] to get average. $avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; does this correctly.
Final Answer:
$avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; -> Option D
Quick Check:
Index 1 and 'scores' key used correctly [OK]
Hint: Use numeric index for array, string keys for inner arrays [OK]
Common Mistakes:
Using string key for outer array
Misspelling 'scores' key
Using wrong index for Bob
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.