0
0
PHPprogramming~30 mins

Multidimensional arrays in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Multidimensional Arrays in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?
Use echo with double quotes and curly braces to insert array values inside the string.