Last active
August 24, 2024 05:11
-
-
Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
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 | |
$movies = [ | |
[ | |
'name' => 'Back to the Future', | |
'releaseYear' => 1985, | |
], | |
[ | |
'name' => "Weekend at Bernie's", | |
'releaseYear' => 1989, | |
], | |
[ | |
'name' => 'Pirates of the Caribbean', | |
'releaseYear' => 2003, | |
], | |
[ | |
'name' => 'Interstellar', | |
'releaseYear' => 2014, | |
], | |
]; | |
// Extra Credit: | |
// Research and apply the array_filter() function on your own. | |
function filterByRecent($movies) | |
{ | |
$filteredMovies = []; | |
foreach ($movies as $movie) { | |
if ($movie['releaseYear'] >= 2000) { | |
$filteredMovies[] = $movie; | |
} | |
} | |
return $filteredMovies; | |
} | |
?> | |
<ul> | |
<?php foreach (filterByRecent($movies) as $movie) : ?> | |
<li> | |
<?= $movie['name'] ?> | |
</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' | |
], | |
]; | |
function filterByAuthor($books, $author) | |
{ | |
$filteredBooks = []; | |
foreach ($books as $book) { | |
if ($book['author'] === $author) { | |
$filteredBooks[] = $book; | |
} | |
} | |
return $filteredBooks; | |
} | |
?> | |
<ul> | |
<?php foreach (filterByAuthor($books, 'Philip K. Dick') 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
Added some select option.. Just for fun...