array_filter


FILTERS elements of an array using a callback function.





This function iterates over each value in the $array passing them to the $callback.

If the $callback = TRUE, the current value from $array is returned into the result ARRAY.

ARRAY keys are preserved.

If no $callback is supplied, all entries of $array equal to FALSE will be removed.



<?php

arr array_filter 
( arr $array [, callable $callback [, int $flag = 0 ]] )
 

where,

$array = The ARRAY to iterate over

$callback 
= The  callback function to be used

$flag 
= To determine what arguments are sent to the callback function

?> 

$array


The ARRAY to iterate over.



$callback


The callback function to be used.



$flag


To determine what arguments are sente to the callback function.

CONSTANT VALUE MEANING DEFAULT
  0 Pass value as the only argument to
callback function instead
0
ARRAY_FILTER_USE_BOTH 1 Pass both value and key as arguments to
callback function instead of the value
ARRAY_FILTER_USE_KEY 2 Pass key as the only argument to
callback function instead of the value
ed48


  1 EXERCISE   

<?php

function odd ($vlu01)
{
    if (
$vlu01%2 == 0)
        {
        return 
$vlu01;
        }
        else
        {
        return (
false);
        }
}

$arr2tst01 = [ 1, 2, 3, 4, 5, 6, 8, 12, 13, 17 ];

$arrfltd01 = array_filter($arr2tst01, 'odd');

echo 
'The given ARRAY<br><br>';

foreach (
$arr2tst01 as $ar01a => $arr01a)
{
    echo 
'[' . $ar01a . '] => ' . $arr01a . '<br>';
}

echo 
'<br><br>The ODD ELEMENTS of the given ARRAY<br><br>';

foreach (
$arrfltd01 as $arf01a => $arrf01a)
{
    echo 
'[' . $arf01a . '] => ' . $arrf01a . '<br>';
}

?> 

 RESULT   

The given ARRAY:
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 8
[7] => 12
[8] => 13
[9] => 17


The ODD ELEMENTS of the given ARRAY:
[1] => 2
[3] => 4
[5] => 6
[6] => 8
[7] => 12


The given ARRAY keys are preserved.


  2 EXERCISE   

<?php

function even ($vlu02)
{
    if (
$vlu02%2 != 0)
        {
        return 
$vlu02;
        }
        else
        {
        return (
false);
        }
}

$arr2tst02 = [ 1, 2, 3, 4, 5, 6, 8, 12, 13, 17 ];

$arrfltd02 = array_filter($arr2tst02, 'even');

echo 
'The given ARRAY<br><br>';

foreach (
$arr2tst02 as $ar02a => $arr02a)
{
    echo 
'[' . $ar02a . '] => ' . $arr02a . '<br>';
}

echo 
'<br><br>The EVEN ELEMENTS of the given ARRAY<br><br>';

foreach (
$arrfltd02 as $arf02a => $arrf02a)
{
    echo 
'[' . $arf02a . '] => ' . $arrf02a . '<br>';
}

?> 

 RESULT   

The given ARRAY:
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 8
[7] => 12
[8] => 13
[9] => 17


The EVEN ELEMENTS of the given ARRAY:
[0] => 1
[2] => 3
[4] => 5
[8] => 13
[9] => 17


The given ARRAY keys are preserved.


  3 EXERCISE   

<?php

$arr2tst03 
= [ 1 => 'string', 2 => NULL, 3 => '', 4 => ' ', 
                        
5 => false, 6 => true, 7 => number_format(6.345, 3) ];

$arrfltd03 = array_filter($arr2tst03);

echo 
'The given ARRAY<br><br>';

foreach (
$arr2tst03 as $ar03a => $arr03a)
{
    echo 
'[' . $ar03a . '] => ' . $arr03a . '<br>';
}

echo 
'<br><br>FALSE, 
                      NULL 
                      or 
                EMPTY STRING, 
              WILL BE REMOVED 
              from the given ARRAY<br><br><pre>'
;

var_dump($arrfltd03);

echo 
'</pre>';

?> 

 RESULT   

The given ARRAY:
[1] => string
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 1
[7] => 6.345


FALSE, NULL or EMPTY STRING,
WAS REMOVED from the given ARRAY:
array(4) {
[1]=>
string(6) "string"
[4]=>
string(1) " "
[6]=>
bool(true)
[7]=>
string(5) "6.345"
}


The given ARRAY keys are preserved.


  4 EXERCISE   

<?php

function odd($var)
{
   return(
$var & 1);
}

function 
even($var)
{
   return(!(
$var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12, 0);
$array3 = array(TRUE, FALSE, NULL);

echo 
"Odd :<br><pre>";
var_dump(array_filter($array1, "odd"));
var_dump(array_filter($array2, "odd"));
var_dump(array_filter($array3, "odd"));
echo 
"</pre>Even:<br><pre>";
var_dump(array_filter($array1, "even"));
var_dump(array_filter($array2, "even"));
var_dump(array_filter($array3, "even"));
echo 
'<br>';
var_dump(array_filter(array()));
echo 
'</pre>';

?>

  5 EXERCISE   

<?php

/*
* Passing different types of array as 'input' argument.
*/

function always_false($input)
{
  return 
false;
}

// callback function returning always true
function always_true($input)
{
  return 
true;
}

echo 
"Testing array_filter() : usage variations -
 <br>- different types of array for 'input' argument.<br>"
;

// different types of 'input' array
$input_values = array(
  array(
0, 1, 2, -1, 034, 0X4A),
  
// integer values
  
array(0.0, 1.2, 1.2e3, 1.2e-3),
  
// float values
  
array('value1', "value2", '', " ", ""),
  
// string values
  
array(true, false, TRUE, FALSE),
  
// bool values
  
array(null, NULL),
  
// null values
  
array(1 => 'one', 'zero' => 0, -2 => "value"),
  
//associative array
  
array("one" => 1, null => 'null', 5.2 => "float", 
                         
true => 1, "" => 'empty'),
  
// associative array with different keys
  
array(1 => 'one', 2, "key" => 'value')
  
// combinition of associative and 
  // non-associative array

);

// loop through each element of 'input' with default callback
for($count = 0; $count < count($input_values); $count++)
{
  echo 
"<br><br><b>Iteration: ".($count + 1). "</b><br><br>";
  
var_dump( array_filter($input_values[$count]) );
  echo 
'<br><br>';
  
var_dump( array_filter($input_values[$count], 
                                                        
'always_true') );
  echo 
'<br><br>';
  
var_dump( array_filter($input_values[$count], 
                                                        
'always_false') );
}


?>

  6 EXERCISE   

<?php

/*
* Passing different types of 
* callback functions to array_filter()
* with parameters and return
* without parameter and with return
* with parameter and without return
* without parameter and without return
*/

echo "Testing array_filter() : usage variation -
              <br>- different 'callback' functions."
;

// Initialize variables
$input = array(0, -1, 2, 
                                
3.4E-3, 
                                
'hello', "value", 
                                
"key" => 4, 
                                
'null' => NULL);

function 
callback1()
{
  return 
1;
}
echo 
"<br><br>Without parameter and with return:<br>";
var_dump( array_filter($input, "callback1") );

// callback function with parameter 
// and without return value
function callback2($input)
{
}
echo 
"<br><br>With parameter and without return:<br>";
var_dump( array_filter($input, "callback2") );

function 
callback3()
{
}
echo 
"<br><br>Without parameter and return:<br>";
var_dump( array_filter($input, "callback3") );

// callback function with parameter 
// and with return value
function callback4($input)
{
  if(
$input > 0 ) {
    return 
true;
  }
  else {
    return 
false;
  }
}
echo 
"<br><br>With parameter and return:<br>";
var_dump( array_filter($input, "callback4") );

?>

  7 EXERCISE   

<?php

/*
* With default callback function argument, 
* array_filter() removes elements which 
* are interpreted as false
* Here Testing all the false array 
* element possibilities
*/

function always_true($input)
{
  return 
true;
}

// callback function always_false
function always_false($input)
{
  return 
false;
}

echo 
"Testing array_filter() : usage variations -
          <br>- different false elements in 'input'.<br><br>"
;

// unset variable
$unset_var = 10;
unset(
$unset_var);

// empty heredoc string
$empty_heredoc =<<<EOT
EOT;

// input array with different false elements
$input = array(
  
false,
  
False,
  
'',
  
"",
  
0,
  
0.0,
  
null,
  
NULL,
  
"0",
  
'0',
  array(),
  !
1,
  
1==2,
  
$empty_heredoc,
  @
$unset_var,
  @
$undefined_var,
);

// With default callback function
var_dump( array_filter($input) );
echo 
'<br><br>';
// With callback function which returns always true
var_dump( array_filter($input, 'always_true') );
echo 
'<br><br>';
// With callback function which returns always false
var_dump( array_filter($input, 'always_false') );

?>

  8 EXERCISE   

<?php

/*
* Passing 'input' array which contains 
* elements as reference to other data
*/

echo "Testing array_filter() : usage variations -
           <br>- 'input' containing references.<br><br>"
;

function 
callback($input)
{
  if(
$input > 5) {
    return 
true;
  }
  else {
    return 
false;
  }
}

// initializing variables
$value1 = array(1, 2, 8);
$value2 = array(5, 6, 4);

echo 
'The given $value1:<br>'; 
var_dump($value1);

echo 
'<br><br>The given $value2:<br>'; 
var_dump($value2);

$input = array(&$value1, 10, &$value2, 'value');

echo 
'<br><br>The $input:<br>';
var_dump($input);

echo 
'<br><br><br>After array_filter($input, \'callback\'):<br>';
// with 'callback' argument
var_dump( array_filter($input, 'callback') );

echo 
'<br><br>After array_filter($input):<br>';
// with default 'callback' argument
var_dump( array_filter($input) );

?>

  9 EXERCISE   

<?php

/*
* Passing different anonymous 
* callback functions with passed 
* by value and reference arguments
*/

echo "Testing array_filter() : usage variations -
         <br>- Anonymous callback functions.<br><br>"
;

$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null);

// anonymous callback function
echo "With regular parameter and statement:<br>";
var_dump( array_filter($input, 
                             function(
$input) { return ($input > 1); }) );

// anonymous callback function with null argument
echo "<br><br>With null argument:<br>";
var_dump( array_filter($input,
                                    function() { return 
true; }) );

// anonymous callback function with 
// argument and null statement
echo "<br><br>With regular argument 
                      and null statement:<br>"
;
var_dump( array_filter($input, 
                                    function(
$input) { }) );

?>

  10 EXERCISE   

<?php

/*
* callback functions are expected
* to return bool value for array_filter()
* here testing callback functions for 
* return values other than bool
*/

echo "Testing array_filter() : usage variations -
<br>- callback function with different return values.<br><br>"
;

$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null, true);

function 
callback1($input)
{
  return 
5;
}
echo 
"callback function with int return value:<br>";
var_dump( array_filter($input, 'callback1') );

// float as return value
function callback2($input)
{
  return 
3.4;
}
echo 
"<br><br>callback function with float return value:<br>";
var_dump( array_filter($input, 'callback2') );

// string as return value
function callback3($input)
{
  return 
'value';
}
echo 
"<br><br>callback function with string return value:<br>";
var_dump( array_filter($input, 'callback3') );

// null as return value
function callback4($input)
{
  return 
null;
}
echo 
"<br><br>callback function with null return value:<br>";
var_dump( array_filter($input, 'callback4') );

// array as return value
function callback5($input)
{
  return array(
8);
}
echo 
"<br><br>callback function with array as return value:<br>";
var_dump( array_filter($input, 'callback5') );

?>