The PHP array_filter can be written like this syntax:

<?php 

 $array = array(1, 2, -1, 3, 4, -1, 5);

  $func  = function( $elem ) {
    if ( $elem != -1 ) {
      return $elem;
    }
  };
  $arr = array_filter($array, $func );

  echo "<pre>";
  print_r($arr);
  echo "</pre>";

The output:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [6] => 5
)

PHP array_filter is a built-in PHP function designed to filter the array elements through a callback and that can be done using a condition inside this function.

The basic structure would be like the below.

array_filter( THE_ARRAY, CALLBACK, FLAG )

In addition, the array elements can be passed as an parameter in the function one by one. So, if you have a condition can exclude some rows from the array if it has a specific unneeded values.

The PHP array_filter() Parameters

The PHP array_filter takes 3 parameters which are as in below list:

  • Array Parameter: a required array to filter it
  • Function Callback: user defined function or anonymous function to send the array rows. That will help you access the array elements row by row
  • Flag: To specify the parameters that already sent to the function is it only with value or keys according to the below flags.
    • ARRAY_FILTER_USE_BOTH When you pass this constant you have to pass two parameters into the function which are the $key and the $value to access them.
    • ARRAY_FILTER_USE_KEY will allow you pass the $key as a parameter in the callback.

Let’s see that in examples.

PHP array_filter() Examples

In the following example, I will delete some unneeded elements from the array.

 <?php 

 $array = array(
   "name"  => "Peter",
   "title" => "Senior Full-Stack Developer",
   "site"  => "CodedTag.com",
   "tasks" => "React Native Program"
 );
 
 function filter_the_array ( $value, $key ){
   return $key != "tasks";
 };
 
 $filtered = array_filter($array, "filter_the_array", ARRAY_FILTER_USE_BOTH );
 
 echo "<pre>";
 print_r($filtered);
 echo "</pre>";

The output:

Array
(
    [name] => Peter
    [title] => Senior Full-Stack Developer
    [site] => CodedTag.com
)

In this example I deleted only one element according to key name. So it returned an array with the deleted element.

But if you only need to pass the key you have to use the ARRAY_FILTER_USE_KEY flag. I will use the same previous example with the ARRAY_FILTER_USE_KEY flag.

 <?php

 function filter_the_array( $key ) {
   return $key != "tasks";
 }
 $filtered = array_filter($array, "filter_the_array", ARRAY_FILTER_USE_KEY );
 
 echo "<pre>";
 print_r($filtered);
 echo "</pre>";

It will show you the same previous output.

Let’s take another example with anonymous function.

<?php 
  
  $array = array(
    10, 6, 15, 8, 1, 2, 55, 99, 19, 14
  );
  $arr = array_filter($array, function( $val ){
     return $val > 10;
  }); 

  echo "<pre>";
  print_r($arr);
  echo "</pre>";

It will return only all numbers greater than 10. So the output would be like the below.

Array
(
    [2] => 15
    [6] => 55
    [7] => 99
    [8] => 19
    [9] => 14
)

Wrapping Up

array_filter is a predefined built-in PHP function used to filter array elements by key or value according to two modes ARRAY_FILTER_USE_BOTH or ARRAY_FILTER_USE_KEY.

The basic expression would be like the below.

array_filter( THE_ARRAY, CALLBACK, FLAG )

Thank you for reading.