wpseek.com
A WordPress-centric search engine for devs and theme authors



array_find_key › WordPress Function

Since6.8.0
Deprecatedn/a
array_find_key ( $array, $callback )
Parameters: (2)
  • (array) $array The array to search.
    Required: Yes
  • (callable) $callback The callback to run for each element.
    Required: Yes
Returns:
  • (int|string|null) The first key in the array that passes the `$callback`, otherwise null.
Defined at:
Codex:

Polyfill for `array_find_key()` function added in PHP 8.4.

Searches an array for the first key that passes a given callback.


Source

function array_find_key( array $array, callable $callback ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
		foreach ( $array as $key => $value ) {
			if ( $callback( $value, $key ) ) {
				return $key;
			}
		}

		return null;
	}
}