New array functions in PHP 8.4 -> array_find, array_find_key, array_any and array_all
Marcos Marcolin • September 16, 2024 • 2 min read
php php84The new PHP version, 8.4, will bring a new set of functions for working with arrays: array_find, array_find_key, array_any, and array_all.
These functions offer a more direct and efficient way to work with arrays, simplifying common operations.
Whether you like it or not, the array is one of PHP's most versatile data structures, being practical and to the point, traits that reflect the essence of the language.
Personally, I prefer writing simple, clean code. That's why I consider these new functions a very welcome addition. They help us avoid unnecessary loops and make code more readable, while keeping the elegance of the language.
array_find
The name says it all: array_find searches for an element in an array and returns the first element that satisfies the criteria defined by the callback.
Function signature:
function array_find(array $array, callable $callback): mixed {}
Usage example:
// Checks whether the value is > 10
function is_greater_than_ten(int $value): bool {
return $value > 10;
}
$array = [5, 8, 12, 3, 15];
array_find($array, 'is_greater_than_ten'); // 12
// With Arrow Functions
array_find($array, fn($value) => $value > 10); // 12
array_find_key
Similar to array_find, the array_find_key function returns the key of the first element that satisfies the callback's criteria.
If no element is found, it returns null.
Function signature:
function array_find_key(array $array, callable $callback): mixed {}
Usage example:
function is_greater_than_ten(int $value): bool {
return $value > 10;
}
$array = [5, 8, 12, 3, 15];
array_find_key($array, 'is_greater_than_ten'); // 2
// With Arrow Functions
array_find_key($array, fn($value) => $value > 10); // 2
array_all
The array_all function checks whether all elements of the array meet the criteria defined by the callback.
If every element returns true for the callback, the function returns true, otherwise, false.
Function signature:
function array_all(array $array, callable $callback): bool {}
Usage example:
$array = [5, 8, 12, 3, 15];
array_all($array, fn($value) => $value > 10); // false
array_all($array, fn($value) => is_numeric($value)); // true
array_any
The array_any function checks whether at least one of the elements of the array meets the callback's criteria.
If at least one element returns true for the callback, the function returns true.
Function signature:
function array_any(array $array, callable $callback): bool {}
Usage example:
$array = [5, 8, 12, 3, 15];
array_any($array, fn($value) => $value > 10); // true
array_any($array, fn($value) => $value > 20); // false
array_any($array, fn($value) => is_numeric($value)); // true
Conclusion
The new functions introduced in PHP 8.4 reinforce the language's commitment to providing tools that make code simpler and more readable.
If you, like me, value straightforward, easy-to-maintain code, these new additions are a great opportunity to improve how you work with arrays in PHP.
Keep an eye on the evolution of the language and take advantage of these features to write even more efficient code.
You can check out the RFC by clicking here.
Until next time!