\Jawira\TheLostFunctions\array_search_callback

array_search_callback - Searches the array for a given value and returns the first element that $callback return true.

Description

array_search_callback(array $array, ?callable $callback = null): mixed

Searches an element using a callback.

Parameters

array
The array.
callback
The callback function to use. If no callback is supplied, the first non empty value will be returned.

Return value

Returns the element if it's found in the array, null otherwise.

Example

use function Jawira\TheLostFunctions\array_search_callback;

$demo = [null, false, '', 'foo', 'bar', '321', 1000];
array_search_callback($demo, fn($item) => is_int($item)); // 1000
array_search_callback($demo, 'is_numeric'); // 321
array_search_callback($demo); // foo