-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociativeLibraryExample.php
More file actions
57 lines (43 loc) · 1.4 KB
/
Copy pathAssociativeLibraryExample.php
File metadata and controls
57 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
$library = [
"Fiction" => [
"The Great Gatsby" => ["author" => "F. Scott Fitzgerald", "year" => 1925],
"1984" => ["author" => "George Orwell", "year" => 1949]
],
"Non-Fiction" => [
"Sapiens" => ["author" => "Yuval Noah Harari", "year" => 2011],
"Educated" => ["author" => "Tara Westover", "year" => 2018]
],
"Science Fiction" => [
"Dune" => ["author" => "Frank Herbert", "year" => 1965],
"Neuromancer" => ["author" => "William Gibson", "year" => 1984]
],
];
$library["Fiction"]["To Kill a Mockingbird"] = ["author" => "Harper Lee", "year" => 1967];
foreach($library as $genre => $books) {
echo "<h1>Genre: $genre</h1>";
echo "<ul>";
foreach ($books as $title => $details) {
echo "<li><b>$title</b> - " . $details["author"] . ", " . $details["year"] . "</li>";
}
echo "</ul>";
}
echo "<br><br>";
/**
* Unset is used to delete any variable. We can also use it
* to remove a value from the array.
* We just pass the variable that we want to delete.
*
* @param mixed $var
*/
unset($library["Non-Fiction"]["Sapiens"]);
echo "<h1>After Deleting: - ";
foreach($library as $genre => $books) {
echo "<h1>Genre: $genre</h1>";
echo "<ul>";
foreach ($books as $title => $details) {
echo "<li><b>$title</b> - " . $details["author"] . ", " . $details["year"] . "</li>";
}
echo "</ul>";
}
?>