PHP 8.4 New Array Functions
PHP 8.4 will be released on November 21, 2024. It'll include property hooks, HTML 5 support, as well as chaining methods on new without additional parentheses — a big one!
PHP 8.4 is set to be released on November 21, 2024. This version has many new features and improvements, including property hooks, HTML 5 support, and chaining methods on new without additional parentheses. Here, we will explore some of the new handy array functions added in PHP 8.4 and compare them with Laravel's Array helpers.
The new array functions include:
- array_find
- array_find_key
- array_any
- array_all
In this article, we will explore these functions and look at the equivalent Laravel helper functions.
1. array_find
The array_find function is a handy tool that helps us find the first element in an array that matches a given condition. If the condition doesn't match this function will return null.
Here's an example. Let's say we have an array of users, and we want to find the user with the role accountant.
The output will be:
If there's no match, array_find will return null. We can make this code cleaner using an arrow function for the callback:
In this case, $serverAdmin will be null because there's no user with the role server_admin.
Laravel Equivalent
We can get the same functionality using Arr::first method.
2. array_find_key
The array_find_key function is similar to array_find but instead of returning the value of the first matching element, it returns the key of the first element that matches the callback condition.
Let's use the same example as before to find the user with the role accountant.
Here, the output will be 2, which is the index of the user with the role accountant.
If there's no match, array_find_key will return null.
Laravel Equivalent
We can get the same functionality using Arr::first method but passing the keys of array as the first argument.
This code finds the first key that matches the condition, just like array_find_key in PHP 8.4.
3. array_any
The array_any function checks if at least one element in an array matches a condition defined in a callback. If any element matches, it returns true; otherwise, it returns false.
Using the same example as before, let's check if there's any user with the role accountant.
The output will be true because there's at least one user with the role accountant. And if there's no user with the role accountant, the output will be false.
Laravel Equivalent
This code converts the $users array into a collection and uses contains to check if any user is an accountant.
4. array_all
The array_all function checks if all elements in an array match a condition defined in a callback. If every element matches, it returns true; otherwise, it returns false.
Using the same example as before, let's check if there's any user with the name Anuj Pandey.
In this case, the output will be true because there's at least one user with the name Anuj Pandey.
Laravel Equivalent
In Laravel, you can achieve the same result using the every method on a collection:
Conclusion
So, this article has given you a good overview of the new array functions coming in PHP 8.4 and how to use then. We also looked how we can achieve similar functionality using Laravel's Array helpers.
Your feedback is valuable to me, so if you'd like to see more content like this in the future, please don't hesitate to let me know.
Keep coding, keep exploring, and keep inspiring. 🐼