Free cookie consent management tool by TermsFeed Generator PHP array filter | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP array filter

PHP array filter

Last Updated on Feb 14, 2023

Array Filter

Array filter is a very useful function that gets 3 arguments

array_filter(array,function,mode)

Array is the array that we are going to filter

Function is a callback function that returns true to keep the item or false to skip the item

Mode is by default 0 which means we use value of each item in our array in the function

Modes

There are 3 modes:

  1. 0 is the default which means the value of each item is given to the callback function
  2. ARRAY_FILTER_USE_KEY means use the key of each item as the function argument
  3. ARRAY_FILTER_USE_BOTH means use value and the key of each item as the function argument

Examples

Let’s see an example 

Here is our array

$array = [
   "twitter" => "Pratham",
   "Feedhive" => "Simon",
   "PHP" => "Amir",
   "Saas" => "Simon",
   "CSS" => "Pratham"
];

We want to filter the ones that have Pratham as the value

$filtered = array_filter($array, function ($value) {
   return $value == 'Pratham';
});
print_r($filtered);
// Array ( [twitter] => Pratham [CSS] => Pratham )

Now let’s filter based on the key and filter the ones that have Feedhive as the key

$filtered = array_filter($array, function ($key) {
   return $key == "Feedhive";
}, ARRAY_FILTER_USE_KEY);
print_r($filtered);
// Array ( [Feedhive] => Simon )

Now let’s use both key and value and filter the ones that have value of Pratham or key of Feedhive

$filtered = array_filter($array, function ($value, $key) {
   return $value == 'Pratham' or $key == "Feedhive";
}, ARRAY_FILTER_USE_BOTH);
print_r($filtered);
// Array ( [twitter] => Pratham [Feedhive] => Simon [CSS] => Pratham )

Conclusion

Now you know about array filter in PHP.

I recommend you to open a PHP files define arrays with different values and keys and try to filter those values and keys with array filter.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array filter in php
  • modes in array filter
  • array filter arguments
  • filter array based on keys
  • filter arrays based on values
  • filter arrays based on key and values

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

3 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
PHP Validate URL
Feb 15, 2023 programming

PHP Validate URL

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the URLs given by the user to make sure the address has a correct format. ...

4 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...