Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active October 26, 2025 07:42
Show Gist options
  • Select an option

  • Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.

Select an option

Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
<!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>
<!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>
@MatimotTheTimoters
Copy link

<?php

/* __________________________________________________
Define a Function
Let's start nice and easy. Define a function called add that returns the sum of two arguments you pass in
*/

function addTwoNumbers($num1, $num2) {
    return $num1 + $num2;
};

/* __________________________________________________
Fetch an Item
We've defined an empty getFirstMovie function for you. It expects an array of movies and will return the first item. Call the function.
*/

$movies = [
    [
        'name' => 'Back to the Future',
        'releaseYear' => 1985,
    ],

    [
        'name' => 'Pirates of the Caribbean',
        'releaseYear' => 2003,
    ],

    [
        'name' => 'Interstellar',
        'releaseYear' => 2014,
    ],
];

function getFirstMovie($movies)
{
    // Write your code here
    return $movies[0];
}

/* __________________________________________________
Create a Filter
Write a function called filterByRecent that filters a given array of movies down to only those that were released in the year 2000 or higher. Save the results to a variable called $recentMovies.
*/

$movies = [
    [
        'name' => 'Back to the Future',
        'releaseYear' => 1985,
    ],

    [
        'name' => 'Pirates of the Caribbean',
        'releaseYear' => 2003,
    ],

    [
        'name' => 'Interstellar',
        'releaseYear' => 2014,
    ],
];

function filterByRecent($movies, $year) {
    $filteredMovies = [];

    foreach ($movies as $movie) {
        if ($movie['releaseYear'] >= $year) {
            $filteredMovies[] = $movie;
        }
    }

    return $filteredMovies;
};

$recentMovies = $filterByRecent($movies, $year);

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