1--TEST-- 2Test array_find() function : basic functionality 3--FILE-- 4<?php 5$array1 = [ 6 "a" => 1, 7 "b" => 2, 8 "c" => 3, 9 "d" => 4, 10 "e" => 5, 11]; 12 13$array2 = [ 14 1, 2, 3, 4, 5 15]; 16 17function even($input) { 18 return $input % 2 === 0; 19} 20 21class EvenClass { 22 public static function even($input) { 23 return $input % 2 === 0; 24 } 25} 26 27var_dump(array_find($array1, fn($value) => $value > 3)); 28var_dump(array_find($array2, fn($value) => $value > 3)); 29var_dump(array_find($array2, fn($value) => $value > 5)); 30var_dump(array_find([], fn($value) => true)); 31var_dump(array_find($array1, fn($value, $key) => $key === "c")); 32var_dump(array_find($array1, fn($value, $key) => false)); 33 34echo '*** Test Exception after found result ***' . PHP_EOL; 35try { 36 var_dump(array_find($array1, function ($value) { 37 if ($value > 1) { 38 throw new Exception("Test-Exception"); 39 } 40 41 return true; 42 })); 43} catch (Exception) { 44 var_dump("Unexpected Exception"); 45} 46 47echo '*** Test aborting with exception ***' . PHP_EOL; 48try { 49 var_dump(array_find($array2, function ($value) { 50 if ($value === 2) { 51 throw new Exception("Test-Exception"); 52 } 53 54 var_dump($value); 55 56 return false; 57 })); 58} catch (Exception) { 59 var_dump("Catched Exception"); 60} 61 62var_dump(array_find($array1, 'even')); 63 64var_dump(array_find($array1, function($value) { 65 // return nothing 66})); 67 68var_dump(array_find($array1, [ 69 'EvenClass', 70 'even' 71])); 72 73var_dump(array_find($array1, "EvenClass::even")); 74?> 75--EXPECT-- 76int(4) 77int(4) 78NULL 79NULL 80int(3) 81NULL 82*** Test Exception after found result *** 83int(1) 84*** Test aborting with exception *** 85int(1) 86string(17) "Catched Exception" 87int(2) 88NULL 89int(2) 90int(2) 91