Skip to content

Instantly share code, notes, and snippets.

@sytnic
Created August 10, 2023 13:00
Show Gist options
  • Save sytnic/30e38f534713554239d10bbe0546f0f6 to your computer and use it in GitHub Desktop.
Save sytnic/30e38f534713554239d10bbe0546f0f6 to your computer and use it in GitHub Desktop.
Lambda Function
<!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>
@sytnic
Copy link
Author

sytnic commented Aug 10, 2023

Result:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment