Hello there! In this lesson, we will apply associative arrays to real-world challenges using PHP. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.
Associative arrays in PHP
are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data using unique keys and efficiently handle larger datasets. Let's understand their efficiency with some practical examples.
Suppose you're asked to manage the cataloging of books in a library. Here, the book ID
serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.
This approach allows us to add, search for, and remove books from our library catalog using just a few lines of PHP
code.
php1<?php 2// Initializing an associative array 3$libraryCatalog = []; 4 5// Details of a book 6$bookId = "123"; 7// Creating an associative array to store details of the book 8$bookDetails = [ 9 "title" => "To Kill a Mockingbird", 10 "author" => "Harper Lee", 11 "year_published" => "1960" 12]; 13 14$libraryCatalog[$bookId] = $bookDetails; // Adding a book to library catalog, where value itself is an associative array 15 16// Searching for a book 17if (array_key_exists($bookId, $libraryCatalog)) { 18 echo "Title: " . $libraryCatalog[$bookId]["title"] . 19 ", Author: " . $libraryCatalog[$bookId]["author"] . 20 ", Year Published: " . $libraryCatalog[$bookId]["year_published"] . "\n"; 21} 22 23unset($libraryCatalog[$bookId]); // Removing a book from the library 24?>
As you can see, associative arrays make the task of cataloging books in the library simpler and more efficient!
Imagine a scenario in which we need to count votes in an election. We employ an associative array, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some PHP
code to better understand this.
php1<?php 2$votesList = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"]; // Cast votes 3$voteCounts = []; // Initializing an associative array 4 5// Counting the votes 6foreach ($votesList as $name) { 7 if (!array_key_exists($name, $voteCounts)) { 8 $voteCounts[$name] = 0; 9 } 10 $voteCounts[$name]++; 11} 12 13foreach ($voteCounts as $name => $count) { 14 echo $name . ": " . $count . "\n"; 15} 16// Prints: Alice: 3, Bob: 2, Charlie: 1 17?>
Associative arrays facilitate the efficient counting of votes.
Finally, consider a task that involves managing a store's inventory. Here, we can use an associative array in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.
php1<?php 2$storeInventory = []; 3$storeInventory["Apples"] = 100; 4$storeInventory["Bananas"] = 80; 5$storeInventory["Oranges"] = 90; // Initializing an inventory 6 7$storeInventory["Pears"] = 50; // Adding a new product to inventory and setting its quantity 8 9$storeInventory["Apples"] += 20; // Updating the number of apples in inventory 10 11$prod = "Apples"; // A product to be checked 12echo "Total " . $prod . " in stock: " . $storeInventory[$prod] . "\n"; 13 14// Check if a product is in stock 15$prod = "Mangoes"; 16if (array_key_exists($prod, $storeInventory)) { // If mangoes exist in inventory 17 echo $prod . " are in stock.\n"; 18} else { // If mangoes don't exist in inventory 19 echo $prod . " are out of stock.\n"; 20} 21?>
Thus, when managing inventory data, associative arrays offer an efficient solution!
In this lesson, we bridged the gap between the theory of associative arrays and their practical applications in PHP
. We explored real-world problems that can be solved using associative arrays and implemented PHP
code to address them.
Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your associative arrays problem-solving skills in PHP
. Happy coding!