We'll explore how to filter an array of books to display only those written by a specific author or by relaeseYear.
Array I'll use in examples
$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'
]
];
We'll start with a basic foreach loop.
foreach($books as $book) {
if($book['author'] === "Andy Weir") {
echo "<a href='#'><li>". $book['name'] ."</li></a>";
}
}
Creating a Filter Function by Author and releaseYear
function filterByAuthor($books) {
$filteredBooks = [];
foreach($books as $book){
if($book['author'] === "Andy Weir") {
$filteredBooks[] = $book;
}
}
return $filteredBooks;
}
function filterByReleaseYear($books) {
$filteredBooks = [];
foreach($books as $book){
if($book['releaseYear'] === 2011) {
$filteredBooks[] = $book;
}
}
return $filteredBooks;
}
?>
<ul>
<?php
foreach(filterByAuthor($books) as $book) {
echo "<a href='#'><li>". $book['name'] ."</li></a>";
}
?>
</ul>
Creating a Generalized Filter Function to add more flexibility
function filter($items, $key, $value) {
$filteredItems = [];
foreach($items as $item){
if($item[$key] === $value) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
$filterByAuthor = filter($books, 'author', 'Andy Weir');
$filteredByReleaseYear = filter($books, 'releaseYear', 2011);
?>
<ul>
<?php
foreach($filterByAuthor as $book) {
echo "<a href='#'><li>". $book['name'] ."</li></a>";
}
?>
</ul>
Using Anonymous Functions for Filtering to add even more flexibility to the conditional statement
function filter($items, $fn) {
$filteredItems = [];
foreach($items as $item){
if($fn($item)) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
$filterByAuthor = filter($books, function($book){
return $book['author'] === 'Andy Weir';
});
$filteredByReleaseYear = filter($books, function($book){
return $book['releaseYear'] >= 2000;
});
?>
<ul>
<?php foreach($filterByAuthor as $book) : ?>
<a href='#'><li> <?= $book['name']; ?> </li></a>
<?php endforeach; ?>
</ul>
<ul>
<?php foreach($filteredByReleaseYear as $book) : ?>
<a href='#'><li> <?= $book['name']; ?> </li></a>
<?php endforeach; ?>
</ul>
PHP build-in array_filter() solves this exactly!
$filterByAuthor = array_filter($books, function($book){
return $book['author'] === 'Andy Weir';
});
$filteredByReleaseYear = array_filter($books, function($book){
return $book['releaseYear'] >= 1950;
});
?>
<ul>
<?php foreach($filterByAuthor as $book) : ?>
<a href='#'><li> <?= $book['name']; ?> </li></a>
<?php endforeach; ?>
</ul>
<ul>
<?php foreach($filteredByReleaseYear as $book) : ?>
<a href='#'><li> <?= $book['name']; ?> </li></a>
<?php endforeach; ?>
</ul>
Top comments (2)
Since PHP 7.4 you can use arrow function. It will make your filter more readable.
yes true, thanks for your value addition