PHP Functions and Filtering

·

2 min read

We can think of a function as a verb to do something, functions are a set of instructions on what we want to do.

Let's filter our book array so we only get back the books that are written by Andry Wei, to solve that we could just use the conditional if but it's not practical, it's hard coded. I will use a function to do the filtering instead after.

<!doctype html>

<html lang="en">

<head>

&lt;meta charset="UTF-8"&gt;



&lt;title&gt;Demo&lt;/title&gt;

</head>

<body>

<?php

$books=[

    \[

            'name'=&gt;'Do Androids Dream of Electric Sheep',

            'author'=&gt;'Philip K. Dick',

             'releaseYear'=&gt;1968,

            'purchaseUrl'=&gt;'[http://example.com](http://example.com)'



    \],

\[

    'name'=&gt;'Project Hail Mary',

    'author'=&gt;'Andy Weir',

    'releaseYear'=&gt;2021,

    'purchaseUrl'=&gt;'[http://example.com](http://example.com)'

\],

\[

        'name'=&gt;'The martian',

         'author'=&gt;'Andy Weir',

          'releaseYear'=&gt;2011,

          'purchaseUrl'=&gt;'[http://example.com](http://example.com)'



\]

];

?>

<ul>

&lt;?php foreach ($books as $book) : ?&gt;

&lt;?php if($book\['author'\]==='Andy Weir') :?&gt;

&lt;li&gt;

    &lt;a href="&lt;?php $book\['purchaseUrl'\];  ?&gt;"&gt;

&lt;?=$book\['name'\]; ?&gt;(&lt;?= $book\['releaseYear'\] ?&gt;) -By &lt;?= $book\['author'\] ?&gt;

    &lt;/a&gt;

&lt;/li&gt;

&lt;?php endif; ?&gt;

&lt;?php endforeach; ?&gt;

</ul>

</body>

</html>

We want a function that takes the books array and filter it and returns an array of books that are written by Andy Weir.

function filteredBooks($books){

$filteredBooks=[];

foreach($books as $book){

if($book['author']==='Andy Weir'){

$filteredBooks[]=$book;

}

}

return $filteredBooks;

}

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

    ]

];
function filterbyAuthor($books){
$filteredBooks=[];
foreach ($books as $book){
    if($book['author']==='Andy Weir'){
        $filteredBooks[]=$book;
    }
}
return $filteredBooks;


}
?>

<ul>
    <?php foreach (filterbyAuthor($books) as $book) : ?>

    <li>
        <a href="<?php $book['purchaseUrl'];  ?>">
    <?=$book['name']; ?>(<?= $book['releaseYear'] ?>) -By <?= $book['author'] ?>
        </a>
    </li>

    <?php endforeach; ?>
</ul>


</body>
</html>

We loop over the array of filteredBooks returned by the function filterbyAuthor($books).

What if don't want to hardcode the author name inside the function ,if($book['author']==='Andy Weir') , well we can add a parameter to our function called $author

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

    ]

];
function filterbyAuthor($books,$author){
$filteredBooks=[];
foreach ($books as $book){
    if($book['author']===$author){
        $filteredBooks[]=$book;
    }
}
return $filteredBooks;


}
?>

<ul>
    <?php foreach (filterbyAuthor($books,'Philip K. Dick') as $book) : ?>

    <li>
        <a href="<?php $book['purchaseUrl'];  ?>">
    <?=$book['name']; ?>(<?= $book['releaseYear'] ?>) -By <?= $book['author'] ?>
        </a>
    </li>

    <?php endforeach; ?>
</ul>


</body>
</html>