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 37try { 38 var_dump( array_filter($small, 'dump', false) ); 39} catch (Throwable $e) { 40 echo "Exception: " . $e->getMessage() . "\n"; 41} 42 43echo "*** Testing array_filter() with various use types ***\n"; 44 45$mixed = array(1 => 'a', 2 => 'b', 'a' => 1, 'b' => 2); 46 47var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_KEY)); 48 49var_dump(array_filter($mixed, 'is_numeric', 0)); 50 51var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_BOTH)); 52 53echo "Done" 54?> 55--EXPECTF-- 56*** Testing array_filter() : usage variations - using array keys in 'callback' *** 570 = 0 581 = 1 592 = -1 603 = 10 614 = 100 625 = 1000 636 = Hello 647 = 65array(0) { 66} 67*** Testing array_filter() : usage variations - 'callback' filters based on key value *** 68array(3) { 69 [5]=> 70 int(1000) 71 [6]=> 72 string(5) "Hello" 73 [7]=> 74 NULL 75} 76*** Testing array_filter() : usage variations - 'callback' expecting second argument *** 77Exception: Too few arguments to function dump(), 1 passed and exactly 2 expected 78*** Testing array_filter() with various use types *** 79array(2) { 80 [1]=> 81 string(1) "a" 82 [2]=> 83 string(1) "b" 84} 85array(2) { 86 ["a"]=> 87 int(1) 88 ["b"]=> 89 int(2) 90} 91 92Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 48 93 94Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 48 95 96Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 48 97 98Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line 48 99array(0) { 100} 101Done 102