Part on Functions finished

This commit is contained in:
Patryk Hegenberg 2022-12-10 14:45:54 +01:00
parent 10571ef7db
commit 9c37ff49a6

View file

@ -13,21 +13,41 @@
[ [
'name' => 'Do Androids Dream of Electric Sheep', 'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick', 'author' => 'Philip K. Dick',
'releaseYear' => 1968,
'purchaseUrl' =>'http://example.com' 'purchaseUrl' =>'http://example.com'
], ],
[ [
'name' => 'Projekt Hail Mary', 'name' => 'Projekt Hail Mary',
'author' => 'Ady Weir', 'author' => 'Andy Weir',
'releaseYear' => 2021,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'releaseYear' => 2011,
'purchaseUrl' => 'http://example.com' 'purchaseUrl' => 'http://example.com'
] ]
]; ];
function filterByAuthor($books, $author){
$filteredBooks = [];
foreach($books as $book) {
if ($book['author'] === $author) {
$filteredBooks[] = $book;
}
}
return $filteredBooks;
}
?> ?>
<?php foreach($books as $book): ?> <ul>
<?php foreach(filterByAuthor($books, 'Philip K. Dick') as $book): ?>
<li> <li>
<a href="<?= $book['purchaseUrl']; ?>"> <a href="<?= $book['purchaseUrl']; ?>">
<?= $book['name']; ?> <?= $book['name']; ?> (<?= $book['releaseYear'] ?>)
</a> </a>
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>
</ul>
</body> </body>
</html> </html>