배열과 람다 함수에 함수와 필터를 추가하는 방법.

PHPz
풀어 주다: 2024-07-19 14:44:37
원래의
749명이 탐색했습니다.

How to add function and filter on an array and lamda function.

<!DOCTYPE html>
<html lang="en">
<head>
    <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>
</head>
<body>
    <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' => 'Philip K. Dick', 'purchaseUrl' => 'http://example.com'],
        ['name' => 'OOP', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://example.com'],
        ['name' => 'Database', 'author' => 'Jeffery', 'purchaseUrl' => 'http://example.com']
    ];

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

    <!-- Display filtered books -->
    <ul>
        <?php foreach ($filteredBooks as $book) : ?>
            <li><?= $book['name']; ?> - by <?= $book['author'] ?></li>
        <?php endforeach; ?>
    </ul>
    <?php
    $sum = function($a, $b) {
        return $a + $b;
    };

   
    echo "Result of lambda function: " . $sum(3, 4);
    ?>
</body>
</html>
로그인 후 복사

위 내용은 배열과 람다 함수에 함수와 필터를 추가하는 방법.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!