-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeach_books.php
60 lines (40 loc) · 1.05 KB
/
foreach_books.php
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
58
<?php
fwrite(STDOUT, "BooksAmillion!!!!!\ngive me a year and i will print all my books published after that year\n");
do {
$cuttoff = trim(fgets(STDIN));
if (is_numeric($cuttoff)) {
$cuttoff = (int)$cuttoff;
} else {
echo "that's not a valid year, please give me another\n";
}
} while (!is_int($cuttoff));
$books = array(
'The Hobbit' => array(
'published' => 1937,
'author' => 'J. R. R. Tolkien',
'pages' => 310
),
'Game of Thrones' => array(
'published' => 1996,
'author' => 'George R. R. Martin',
'pages' => 835
),
'The Catcher in the Rye' => array(
'published' => 1951,
'author' => 'J. D. Salinger',
'pages' => 220
),
'A Tale of Two Cities' => array(
'published' => 1859,
'author' => 'Charles Dickens',
'pages' => 544
)
);
foreach ($books as $title => $tinfo) {
if ($tinfo['published'] > $cuttoff) {
echo $title. PHP_EOL;
foreach ($tinfo as $detail => $info) {
echo " $detail: $info\n";
}
}
}