1--TEST-- 2Test array_filter() function : usage variations - using the array keys inside 'callback' 3--FILE-- 4<?php 5/* 6* Using array keys as an argument to the 'callback' 7*/ 8 9echo "*** Testing array_filter() : usage variations - using array keys in 'callback' ***\n"; 10 11$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null); 12$small = array(123); 13 14function dump($value, $key) 15{ 16 echo "$key = $value\n"; 17} 18 19var_dump( array_filter($input, 'dump', true) ); 20 21echo "*** Testing array_filter() : usage variations - 'callback' filters based on key value ***\n"; 22 23function dump2($value, $key) 24{ 25 return $key > 4; 26} 27 28var_dump( array_filter($input, 'dump2', true) ); 29 30echo "*** Testing array_filter() : usage variations - 'callback' expecting second argument ***\n"; 31 32try { 33 var_dump( array_filter($small, 'dump', false) ); 34} catch (Throwable $e) { 35 echo "Exception: " . $e->getMessage() . "\n"; 36} 37 38echo "*** Testing array_filter() with various use types ***\n"; 39 40$mixed = array(1 => 'a', 2 => 'b', 'a' => 1, 'b' => 2); 41 42var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_KEY)); 43 44var_dump(array_filter($mixed, 'is_numeric', 0)); 45 46try { 47 var_dump(array_filter($mixed, 'is_numeric', ARRAY_FILTER_USE_BOTH)); 48} catch (TypeError $e) { 49 echo $e->getMessage(), "\n"; 50} 51 52echo "Done" 53?> 54--EXPECT-- 55*** Testing array_filter() : usage variations - using array keys in 'callback' *** 560 = 0 571 = 1 582 = -1 593 = 10 604 = 100 615 = 1000 626 = Hello 637 = 64array(0) { 65} 66*** Testing array_filter() : usage variations - 'callback' filters based on key value *** 67array(3) { 68 [5]=> 69 int(1000) 70 [6]=> 71 string(5) "Hello" 72 [7]=> 73 NULL 74} 75*** Testing array_filter() : usage variations - 'callback' expecting second argument *** 76Exception: Too few arguments to function dump(), 1 passed and exactly 2 expected 77*** Testing array_filter() with various use types *** 78array(2) { 79 [1]=> 80 string(1) "a" 81 [2]=> 82 string(1) "b" 83} 84array(2) { 85 ["a"]=> 86 int(1) 87 ["b"]=> 88 int(2) 89} 90is_numeric() expects exactly 1 argument, 2 given 91Done 92