1--TEST-- 2Test array_filter() function : usage variations - using the array keys inside 'callback' 3--FILE-- 4<?php 5/* Prototype : array array_filter(array $input [, callback $callback [, bool $use_type = ARRAY_FILTER_USE_VALUE]]) 6 * Description: Filters elements from the array via the callback. 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11* Using array keys as an argument to the 'callback' 12*/ 13 14echo "*** Testing array_filter() : usage variations - using array keys in 'callback' ***\n"; 15 16$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null); 17$small = array(123); 18 19function dump($value, $key) 20{ 21 echo "$key = $value\n"; 22} 23 24var_dump( array_filter($input, 'dump', true) ); 25 26echo "*** Testing array_filter() : usage variations - 'callback' filters based on key value ***\n"; 27 28function dump2($value, $key) 29{ 30 return $key > 4; 31} 32 33var_dump( array_filter($input, 'dump2', true) ); 34 35echo "*** Testing array_filter() : usage variations - 'callback' expecting second argument ***\n"; 36 37var_dump( array_filter($small, 'dump', false) ); 38 39echo "*** Testing array_filter() with various use types ***\n"; 40 41$mixed = array(1 => 'a', 2 => 'b', 'a' => 1, 'b' => 2); 42 43var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_KEY)); 44 45var_dump(array_filter($mixed, 'is_numeric', 0)); 46 47var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_BOTH)); 48 49echo "Done" 50?> 51--EXPECTF-- 52*** Testing array_filter() : usage variations - using array keys in 'callback' *** 530 = 0 541 = 1 552 = -1 563 = 10 574 = 100 585 = 1000 596 = Hello 607 = 61array(0) { 62} 63*** Testing array_filter() : usage variations - 'callback' filters based on key value *** 64array(3) { 65 [5]=> 66 int(1000) 67 [6]=> 68 string(5) "Hello" 69 [7]=> 70 NULL 71} 72*** Testing array_filter() : usage variations - 'callback' expecting second argument *** 73 74Warning: Missing argument 2 for dump() in %s on line %d 75 76Notice: Undefined variable: key in %s on line %d 77 = 123 78array(0) { 79} 80*** Testing array_filter() with various use types *** 81array(2) { 82 [1]=> 83 string(1) "a" 84 [2]=> 85 string(1) "b" 86} 87array(2) { 88 ["a"]=> 89 int(1) 90 ["b"]=> 91 int(2) 92} 93 94Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 44 95 96Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 44 97 98Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 44 99 100Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 44 101array(0) { 102} 103Done 104