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 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 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> |
juanmarcoso
commented
Nov 29, 2023
<?php
$books = [
[
'title' => 'Hail Mary',
'author' => 'Drew',
'purchaseUrl' => 'https://example.com',
'releaseYear' => 2001
],
[
'title' => 'Project Hail Mary',
'author' => 'Wer',
'purchaseUrl' => 'https://example.com',
'releaseYear' => 1990
],
[
'title' => 'Projects Hail Mary',
'author' => 'Wer',
'purchaseUrl' => 'https://example.com',
'releaseYear' => 2020
],
];
$filterBooks = array_filter($books, function ($book) {
return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020;
});
?>
<?php foreach ($filterBooks as $book) : ?>
<li>
<?= $book['title'] ?>
</li>
<?php endforeach; ?>
"The dragon fly", "release_year" => 1978, "author" => "James Dio", "purchase_url" => "http://example.com"],
["book_title" => "The serial killer", "release_year" => 1958, "author" => "Jackon Duo", "purchase_url" => "http://example.com"],
["book_title" => "Politics", "release_year" => 1988, "author" => "Ellen Marshall", "purchase_url" => "http://example.com"],
["book_title" => "Sweet heart", "release_year" => 2022, "author" => "James Dio", "purchase_url" => "http://example.com"]
];
function filter($items, $fn)
{
$filteredItems = [];
foreach ($items as $item) {
if ($fn($item)) {
$filteredItems[] = $item;
}
}
return $filteredItems;
};
$filteredBooks = array_filter($books, function ($book) {
return $book["author"] === "Ellen Marshall";
});
?>
<title>Library Books</title>
<ul>
<h2>Selected titles</h2>
<?php foreach ($filteredBooks as $item) : ?>
<li>Click book link to purchase:
<a href="<?= $item["purchase_url"] ?>">
<?= $item["author"] ?> >>
<?= $item["book_title"] ?>
(<?= $item["release_year"] ?>)
</a>
</li>
<?php endforeach; ?>
</ul>
$filteredBooks = array_filter($books, function ($book) { return $book['year'] >= 1950 && $book['year']<= 2020; });
<?php
$movies = [
[
'name' => 'Fast n Furious',
'releaseDate' => 2002
],
[
'name' => 'Terminator 2',
'releaseDate' => 1998
],
[
'name' => 'Power Book II',
'releaseDate' => 2020
],
];
/*function filterByDate($movies, $releaseDate) {
$filterMovies = [];
foreach ($movies as $movie) {
if ($movie['releaseDate'] >= $releaseDate) {
$filterMovies[] = $movie;
}
}
return $filterMovies;
}*/
$filterByDate = array_filter($movies, function($movie){
return $movie['releaseDate'] >= 1950 && $movie['releaseDate'] <= 2020;
});
?>
<ul>
<?php foreach ($filterByDate as $movie): ?>
<li>
<?= $movie['name'] ?>
</li>
<?php endforeach; ?>
</ul>
</body>
$filteredBooks = array_filter($books, function ($book) { return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020; });
<!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;
});
?>
<h1>Recommended Books</h1>
<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>
<?php
$cars = [
['name' => "Supra",
'date' => 1990],
[
'name' => 'Marcedes Benz',
'date' => 1901
],
[
'name' => 'Ferrari',
'date' => 1927
]
];
function filter($items , $fn){
$filterItems= [];
foreach($items as $item){
if($fn($item)){
$filterItems[] = $item;
}
}
return $filterItems;
};
$findbyDate = array_filter($cars, function ($car){
return $car['date'] > 1910 && $car['date'] <= 1990;
});
?>
<ul>
<?php foreach($findbyDate as $car): ?>
<li>
<?= $car['name'] ?>
</li>
<?php endforeach; ?>
</ul>
</body>
<!doctype html>
<title>Demo</title><h1>A list of Movies:</h1>
<?php
$books = [
[
'name' => 'Inception: The Shooting Script',
'author' => 'Christopher Nolan & Jonathan Nolan',
'releaseYear' => 2010,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'The Nightingale',
'author' => 'Kristin Hannah',
'releaseYear' => 2021,
'purchaseUrl' => 'http://example.com'
],
[
'name' => '1000 Years of Joys and Sorrows',
'author' => 'Xiaolu Guo',
'releaseYear' => 2021,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Rita Hayworth and Shawshank Redemption',
'author' => 'Stephen King',
'releaseYear' => 1982,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Batman: The Dark Knight Returns',
'author' => 'Frank Miller',
'releaseYear' => 1986,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Pulp Fiction: A Quentin Tarantino Screenplay',
'author' => 'Quentin Tarantino',
'releaseYear' => 1994,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Forrest Gump',
'author' => 'Winston Groom',
'releaseYear' => 1986,
'purchaseUrl' => 'http://example.com'
]
];
function filter($items, $fn)
{
$filteredItems = [];
foreach ($items as $item) {
if ($fn($item)) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
$filteredBooks = filter($books, function ($book) {
return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020;
});
?>
<?php foreach ($filteredBooks as $book) : ?>
<ul>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>) - By <?= $book['author'] ?>
</a>
</li>
</ul>
<?php endforeach; ?>
<title>Document</title>
'Forrest Gump',
'director' => 'Gus Van Sant',
'released' => '1930'
],
[
'name' => 'Shawshank Redemption',
'director' => 'Frank Darabont',
'released' => '1950'
],
[
'name' => 'Good Will Hunting',
'director' => 'Gus Van Sant',
'released' => '2000'
],
[
'name' => 'Fight Club',
'director' => 'Gus Van Sant',
'released' => '2030'
],
[
'name' => 'American Phycho',
'director' => 'Gus Van Sant',
'released' => '2015'
],
];
function filter($items, $fn)
{
$filteredItems = [];
foreach ($items as $item) {
if ($fn($item)) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
//we can handle the comparison by ourselves
$filteredMovie = filter($movies, function ($movie) {
return $movie['released'] >= 1950 && $movie['released'] <= 2020;
});
?>
<ul>
<?php
foreach ($filteredMovie as $movie) : ?>
<li>
<?= $movie['name'] ?> (<?= $movie['released'] ?>)
</li>
<?php endforeach; ?>
</ul>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment