• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php array_filter() – Filter Array Elements with Function in php

php array_filter() – Filter Array Elements with Function in php

April 13, 2022 Leave a Comment

The php array_filter() function gives us the ability to filter an array with a callback function. You can filter both values and keys with array_filter()

function greater_than_5($var) {
    return $var > 5;
}

$array = array(1,2,3,4,5,6,7,8,9,10);

print_r(array_filter($array, "greater_than_5"));

// Output:
Array
(
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.

One such operation is filtering an array and removing elements based on conditions.

The php array_filter() function gives us the ability to filter arrays based on callback functions. The callback function should check each array element with an expression and return true or false.

With array_filter(), we can remove elements from our arrays in php with logical expressions.

Below is a simple example showing how you can use array_filter() to filter out all elements of an array where the elements are not greater than 5.

function greater_than_5($var) {
    return $var > 5;
}

$array = array(1,2,3,4,5,6,7,8,9,10);

print_r(array_filter($array, "greater_than_5"));

// Output:
Array
(
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

Your callback function can be any function which returns a boolean value. You can also define and pass the callback function directly to array_filter().

Below is another example of how you can define and pass a callback function to array_filter().

$array = array("string","hi","berries","soup");

print_r(array_filter($array, function($var) {
    return strlen($var) > 5;
}));

// Output:
Array
(
    [0] => string
    [2] => berries
)

Removing All Empty Elements from a Given Array with php array_filter() Function

If you don’t pass a callback function, then array_filter() removes all empty elements from the given array.

Empty elements include 0, null, false, empty strings, etc.

Below is an example in php of how to use array_filter() to remove all empty elements by not passing a callback function.

$array = array(0, null, false, 1, '', '0', "pink", "bear");

print_r(array_filter($array));

// Output:
Array
(
    [3] => 1
    [6] => pink
    [7] => bear
)

Filtering an Array by Key with php array_filter() Function

You can also filter an array by its keys with array_filter(). There is an optional parameter called ‘mode’ that allows us to filter the array by its keys or by both its keys and values.

Passing ‘ARRAY_FILTER_USE_KEY’ will apply the callback function to an array’s keys. ‘ARRAY_FILTER_USE_BOTH’ applies the callback function to both an array’s values and keys.

Below is an example of how you can filter an array in php by the keys of the array with the php array_filter() function.

$array = array("word" => "string", "greeting" => "hi", "fruit" => "berries", "dinner" => "soup");

print_r(array_filter($array, function($k) {
    return strlen($k) > 5;
}, ARRAY_FILTER_USE_KEY));

// Output:
Array
(
    [greeting] => hi
    [dinner] => soup
)

As you can see, we only removed the elements where the keys didn’t have a string length of at least 5.

Below is another example of how you can filter an array by applying conditions to both the keys and values of the array.

$array = array("word" => "string", "greeting" => "hi", "fruit" => "berries", "dinner" => "soup");

print_r(array_filter($array, function($v, $k) {
    return strlen($k) > 5 || $v == "string";
}, ARRAY_FILTER_USE_BOTH));

// Output:
Array
(
    [word] => string
    [greeting] => hi
    [dinner] => soup
)

Hopefully this article has been useful for you to learn how to use the php array_filter() function to filter the elements from an array with a callback function.

Other Articles You'll Also Like:

  • 1.  php is_array() Function – Check if Variable is Array in php
  • 2.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 3.  Remove First Character from String Using php
  • 4.  php json_encode() Function – Get JSON Representation of Variables in php
  • 5.  How to Get Current System IP Address in php
  • 6.  php Delete Files with php Unlink Function
  • 7.  PHP random_int() Function – Generate Random Integers in PHP
  • 8.  Delete Cookie Using php
  • 9.  php pi – Get Value of pi Using php pi() Function
  • 10.  Remove HTML Tags from String in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy