Part on Lambda Functions finished

This commit is contained in:
Patryk Hegenberg 2022-12-10 15:07:08 +01:00
parent 9c37ff49a6
commit a45a77f0ae

View file

@ -29,19 +29,13 @@
'purchaseUrl' => 'http://example.com' 'purchaseUrl' => 'http://example.com'
] ]
]; ];
$filteredItems = array_filter($books, function($book) {
return $book['author'] == 'Andy Weir';
});
function filterByAuthor($books, $author){
$filteredBooks = [];
foreach($books as $book) {
if ($book['author'] === $author) {
$filteredBooks[] = $book;
}
}
return $filteredBooks;
}
?> ?>
<ul> <ul>
<?php foreach(filterByAuthor($books, 'Philip K. Dick') as $book): ?> <?php foreach($filteredItems as $book): ?>
<li> <li>
<a href="<?= $book['purchaseUrl']; ?>"> <a href="<?= $book['purchaseUrl']; ?>">
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>) <?= $book['name']; ?> (<?= $book['releaseYear'] ?>)