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

PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 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 ...