30 seconds of PHP

  Back to Home

findLast

arraybeginner

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);
}