2024-07-15

如何在数组和 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 -->
    
登录后复制
           
               

  • = $book[‘name’]; ?> – by = $book[‘author’] ?>
  •        
       

   

以上就是如何在数组和 lamda 函数上添加函数和过滤器的详细内容,更多请关注php中文网其它相关文章!

https://www.php.cn/faq/878219.html

发表回复

Your email address will not be published. Required fields are marked *