Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created September 29, 2022 17:36
Show Gist options
  • Save JeffreyWay/6851dc65ec5ecfc1eba2213cee42ab70 to your computer and use it in GitHub Desktop.
Save JeffreyWay/6851dc65ec5ecfc1eba2213cee42ab70 to your computer and use it in GitHub Desktop.
PHP for Beginners, Episode 9 - Lambdas and Flexibility
<!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>
<!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
Copy link

<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