summaryrefslogtreecommitdiff
path: root/ext/standard/tests/array/array_filter_basic.phpt
blob: b2a51ab2c60c3f346edce226fad3bf0885248382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
--TEST--
Test array_filter() function : basic functionality
--FILE--
<?php
/* Prototype  : array array_filter(array $input [, callback $callback])
 * Description: Filters elements from the array via the callback.
 * Source code: ext/standard/array.c
*/


echo "*** Testing array_filter() : basic functionality ***\n";


// Initialise all required variables
$input = array(1, 2, 3, 0, -1);  // 0 will be considered as FALSE and removed in default callback

/* Callback function
 * Prototype : bool even(array $input)
 * Parameters : $input - input array each element of which will be checked in function even()
 * Return type : boolean - true if element is even and false otherwise
 * Description : This function takes array as parameter and checks for each element of array.
 *              It returns true if the element is even number else returns false
 */
function even($input)
{
  return ($input % 2 == 0);
}

// with all possible arguments
var_dump( array_filter($input,"even") );

// with default arguments
var_dump( array_filter($input) );

echo "Done"
?>
--EXPECT--
*** Testing array_filter() : basic functionality ***
array(2) {
  [1]=>
  int(2)
  [3]=>
  int(0)
}
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [4]=>
  int(-1)
}
Done