-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforEach_books.php
50 lines (48 loc) · 1.22 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
<?php
$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
),
'Lord of the Rings' => array(
'published' => 1968,
'author' => 'J. R. R. Tolkien',
'pages' => 1178
)
);
foreach ($books as $key => $value) {
if ($value['published'] >= 1950 && $value['pages'] > 300){
echo "Title: " . $key .PHP_EOL;
echo "Published: " . $value['published'] .PHP_EOL;
echo "Author: " . $value['author'] .PHP_EOL;
echo "Pages: " . $value['pages'] .PHP_EOL;
}
}
$x = 0;
$i = 0;
$y = 0;
foreach ($books as $key => $value) {
$i += $value['pages'];
$x++;
$y += $value['published'];
}
$i = $i / $x;
$y = $y / $x;
echo "Average publication year: ".$i.PHP_EOL;
echo "Average amount of pages: ".$y.PHP_EOL;