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

使用样例

findLastIndex([1, 2, 3, 4], function ($n) {
  return ($n % 2) === 1;
});
// 2