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',
'author' => 'Philip K. Dick',
'releaseYear' => 1968,
'purchaseUrl' =>'http://example.com'
],
[
'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'
]
];
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>
<a href="<?= $book['purchaseUrl']; ?>">
<?= $book['name']; ?>
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>)
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>