卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章66628本站已运行425

如何在数组和 lamda 函数上添加函数和过滤器

如何在数组和 lamda 函数上添加函数和过滤器




    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            display: grid;
            place-items: center;
            font-family: sans-serif;
            height: 100px; 
            margin: 20px; 
        }
    </style>
    <h1>You have read in dark mode </h1>

    <?php     
    function filterBooksByAuthor($books, $author) {
        $filteredBooks = array_filter($books, function($book) use ($author) {
            return $book['author'] == $author;
        });
        return $filteredBooks;
    }

    $books = [
        ['name' => 'Web', 'author' =&gt; 'Philip K. Dick', 'purchaseUrl' =&gt; 'http://example.com'],
        ['name' =&gt; 'OOP', 'author' =&gt; 'Andy Weir', 'purchaseUrl' =&gt; 'http://example.com'],
        ['name' =&gt; 'Database', 'author' =&gt; 'Jeffery', 'purchaseUrl' =&gt; 'http://example.com']
    ];

    $filteredBooks = filterBooksByAuthor($books, 'Andy Weir');
    ?&gt;

    <!-- Display filtered books -->
    
            <?php foreach ($filteredBooks as $book) : ?>            
  • = $book['name']; ?> - by = $book['author'] ?>
  •         <?php endforeach; ?>    
    <?php     $sum = function($a, $b) {         return $a + $b;     };         echo "Result of lambda function: " . $sum(3, 4);     ?>
卓越飞翔博客
上一篇: Golang框架容器化部署和管理指南
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏