Created
August 10, 2023 13:00
-
-
Save sytnic/30e38f534713554239d10bbe0546f0f6 to your computer and use it in GitHub Desktop.
Lambda Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Demo</title> | |
</head> | |
<body> | |
<?php | |
$books = [ | |
[ | |
'name' => 'Do Androids Dream of Electric Sheep', | |
'author' => 'Philip K. Dick', | |
'releaseYear' => 1968, | |
'purchaseUrl' => 'http://example.com' | |
], | |
[ | |
'name' => 'Project Hail Mary', | |
'author' => 'Andy Weir', | |
'releaseYear' => 2021, | |
'purchaseUrl' => 'http://example.com' | |
], | |
[ | |
'name' => 'The Martian', | |
'author' => 'Andy Weir', | |
'releaseYear' => 2011, | |
'purchaseUrl' => 'http://example.com' | |
], | |
]; | |
// Лямбда-функция | |
$filterByAuthor = function ($books, $author) | |
{ | |
$filteredBooks = []; | |
foreach ($books as $book) { | |
if ($book['author'] === $author) { | |
$filteredBooks[] = $book; | |
} | |
} | |
return $filteredBooks; | |
}; | |
$filteredBooks = $filterByAuthor($books, 'Andy Weir'); | |
?> | |
<ul> | |
<?php foreach ($filteredBooks as $book) : ?> | |
<li> | |
<a href="<?= $book['purchaseUrl'] ?>"> | |
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>) - By <?= $book['author'] ?> | |
</a> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: