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 DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP Interface
Mar 22, 2023 programming

PHP Interface

Today we are going to talk about interfaces in PHP. Interface is like a skeleton for the class. Interface is an important topic in OOP and it helps you manage your code better. ...

6 Min Read Read More
PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers
Sep 10, 2024 programming

Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers

While PHP and Python share several core programming concepts, transitioning between the two requires understanding the key differences in syntax, paradigms, and best practices. ...

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