CLI_Command::array_find
Returns the the first element of the passed array for which the callback returns true.
Polyfill for the array_find() introduced in PHP 8.3.
Method of the class: CLI_Command{}
No Hooks.
Returns
Mixed. First array element for which the callback returns true, null otherwise.
Usage
// private - for code of main (parent) class only $result = $this->array_find( $arr, $callback );
- $arr(array) (required)
- Array to search.
- $callback(callable) (required)
- The callback function for each element in the array.
CLI_Command::array_find() CLI Command::array find code WP-CLI 2.13.0-alpha
private function array_find( $arr, $callback ) {
if ( function_exists( '\array_find' ) ) {
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_findFound
return \array_find( $arr, $callback );
}
foreach ( $arr as $key => $value ) {
if ( $callback( $value, $key ) ) {
return $value;
}
}
return null;
}