Returns the last element for which the provided function returns a truthy value.
Use array_filter()
to remove elements for which $func
returns falsy values, array_pop()
to get the last one.
代码实现
function findLast($items, $func)
{
$filteredItems = array_filter($items, $func);
return array_pop($filteredItems);
}
使用样例
findLast([1, 2, 3, 4], function ($n) {
return ($n % 2) === 1;
});
// 3