Created
September 29, 2022 17:36
-
-
Save JeffreyWay/6851dc65ec5ecfc1eba2213cee42ab70 to your computer and use it in GitHub Desktop.
PHP for Beginners, Episode 9 - Lambdas and Flexibility
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' | |
| ], | |
| ]; | |
| $filteredBooks = array_filter($books, function ($book) { | |
| return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020; | |
| }); | |
| ?> | |
| <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> |
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' | |
| ], | |
| ]; | |
| $filteredBooks = array_filter($books, function ($book) { | |
| return $book['author'] === '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> |
saifull03
commented
Jul 16, 2024
<title>Document</title>
'Forrest Gump',
'director' => 'Gus Van Sant',
'released' => '1930'
<?php
/* __________________________________________________
Array Filter
Use PHP's array_filter function to filter the list books to only those written by "Andy Weir."
*/
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'releaseYear' => 1968,
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'releaseYear' => 2021,
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'releaseYear' => 2011,
],
];
$filteredBooks = array_filter($books, function($book) {
return $book['author'] === 'Andy Weir';
});
/* __________________________________________________
Where Between
Filter the books to only the ones that were originally published between the years 1950 and 2000.
*/
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'releaseYear' => 1968,
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'releaseYear' => 2021,
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'releaseYear' => 2011,
],
];
// Hint - the PHP equivalent of "and" is &&.
function filter($items, callable $callback) {
$filteredItems = [];
foreach ($items as $item) {
if ($callback($item)) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
$filteredBooks = filter($books, fn($book) => {
$bookWithinRange = ($book['releaseYear'] >= 1950) &&
($book['releaseYear'] <= 2000);
if ($bookWithinRange) {
return $book;
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment