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>
@K1LLERB0T
Copy link

$filteredBooks = array_filter($books, function ($book) { return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020; });

@binsarkiel
Copy link


<!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>

@SIFAT-AOC
Copy link

  

<?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>

@Rofiaziztr
Copy link

<!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; ?>

@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