Working with PHP Array Filter Function
In this tutorial we are going to play around the PHP array filter function called array_filter()
and it is very useful when we want to filter php arrays based on certain condition
PHP array function is a built in function which is use to Filters elements of an array using a callback function.
So basically the array_filter()
function uses the callback function to define the condition based on we want to filter an array.
The callback function is Iterates over each items for the array and if the call back function returns true the current items will be returned to the result of the array filter function.
Here is the syntax of using array function in php, so first parameter is the input array which want to filter and the second parameter is the call function which we used to define condition.
array_filter ( array $input [, callable $callback [, int $flag = 0 ]] ) : array
Last argument is optional parameter which required constants ARRAY_FILTER_USE_KEY
and ARRAY_FILTER_USE_BOTH
Let me give you quick and useful example here to work around the array_filter function, so here I am going to create a Post class along with few properties and will create array of Post Objects as showing below:
<?php
class Post
{
public $title;
public $body;
public $author;
public $published;
public function __construct($title, $body, $author, $published)
{
$this->title = $title;
$this->body = $body;
$this->author = $author;
$this->published = $published;
}
}
$posts = [
new Post('Test Post 1', 'Body of post test 1', 'YK', true),
new Post('Test Post 2', 'Body of post test 2', 'YK', false),
new Post('Test Post 3', 'Body of post test 3', 'YK', true),
new Post('Test Post 4', 'Body of post test 4', 'AB', false)
];
If you see we have added four objects of Post
class into the $posts
variables now let’s take quick examples
Here is the example of filtering an array using array_filter
function:
$published = array_filter($posts, function ($post) {
return $post->published;
});
var_dump($published);
If you run the var_dump on this new filter array you should the following result.
Similarly we can use the same function to filter the array but the other direction.
$unpublished = array_filter($posts, function ($post) {
return !$post->published;
});
var_dump($unpublished);
This is one more additional example to filter from, here we are filtering based of the author of the post.
$posts = array_filter($posts, function ($post) {
return $post->author == 'AB';
});
var_dump($posts);
The Most important lesson you should get from this tutorial is to avoid using foreach to filter your array in php
I have given the example using array which is having collection objects but you might have different array
Like array with collection of key value pair, associative or the multidimensional array.
No matter which array you have you can alway use array_filter()
function to filter from depending on you requirements.
Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…
Learn how to use php array map function with easy and essential tutorial to modify…
Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…
What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…
This tutorial provide ultimate list of package those are top 10 on packagist and super…
This tutorial provides a perfect solution on how to protect and secure pdf file in…