30 seconds of PHP

  Back to Home

findLastIndex

arraybeginner

Returns the index of the last element for which the provided function returns a truthy value.

Use array_keys() and array_filter() to remove elements for which $func returns falsy values, array_pop() to get the last one.

function findLastIndex($items, $func)
{
  $keys = array_keys(array_filter($items, $func));

  return array_pop($keys);
}