PHP 8.4, the latest release in the PHP series, introduces a range of exciting new features and improvements aimed at enhancing performance and developer experience. Among the key highlights are the addition of readonly classes, which simplify immutability, and the support for intersection types, allowing developers to define more precise type constraints. Additionally, PHP 8.4 offers enhanced match expressions, making conditional logic even more powerful. With further optimizations in performance and several bug fixes, PHP 8.4 continues the evolution of one of the most popular server-side scripting languages, providing developers with more tools for writing clean, efficient code.
This article will talk about the new array functions in PHP 8.4, there are:
- array_find
- array_find_key
- array_any
- array_all
I will breakthrough those new array functions.
array_find function
The array_find function returns the value of the first element that matches the criteria defined in the callback. If no element matches the callback, the function returns null.
Let see the example below:
$products = [ [ 'name' => 'Sony WH-1000XM5', 'type' => 'Headphones', 'barcode' => 654321, ], [ 'name' => 'Apple iPad Air', 'type' => 'Tablet', 'barcode' => 987654, ], [ 'name' => 'Google Pixel 8', 'type' => 'Phone', 'barcode' => 246810, ], ]; // Find the product with barcode 654321 $findProduct = array_find( array: $products, callback: function (array $product): bool { return $product['barcode'] == 654321; }, );
If you run the code above, the result will be:
[ 'name' => 'Sony WH-1000XM5', 'type' => 'Headphones', 'barcode' => 654321, ]
If no element matches the callback, the function will return null.
array_find_key function
This function works like array_find, but rather than returning the value of the first element that meets the callback condition, it returns the key of the first matching element.
I will use the $products above as an example, with usage of array_find_key, take a look to the code below:
$products = [ [ 'name' => 'Sony WH-1000XM5', 'type' => 'Headphones', 'barcode' => 654321, ], [ 'name' => 'Apple iPad Air', 'type' => 'Tablet', 'barcode' => 987654, ], [ 'name' => 'Google Pixel 8', 'type' => 'Phone', 'barcode' => 246810, ], ]; // Find the key of the product with barcode 987654 $findProduct = array_find_key( array: $products, callback: fn (array $product): bool => $product['barcode'] === 987654; }, );
After running the code, the result of $findProduct will be 1, because the product is the second element in the array. If no element matches the callback, the function will return null.
array_any function
The array_any function checks whether at least one element in the array satisfies the condition specified in the callback. It returns true if any element meets the criteria; otherwise, it returns false if none do.
Still using the $products array, let's try to find any product with type Phone.
$productsArePhone = array_any( array: $products, callback: fn (array $product): bool => $product['type'] === 'Phone', );
In this scenario, $productsArePhone will be true since at least one product in the array is a phone. If no product meets the callback condition, the function will return false.
array_all function
The array_all function works similarly to array_any, but instead of verifying if at least one element meets the callback criteria, it checks whether all elements satisfy the condition. If every element matches the callback, the function returns true. If any element fails to meet the condition, the function returns false.
Let's check if all products in $products are Phone.
$allProductsArePhone = array_all( array: $products, callback: fn (array $product): bool => $product['type'] === 'Phone', );
In this case, $allProductsArePhone will be false because not all the products in the array are phones. If even one product doesn't meet the condition, the function will return false.
Conclusion
So, those are the new array function in PHP 8.4. Hopefully they can help you to solve some array checking or filtering in your PHP code.
Keep coding to sharpen your skills even more, Happy coding guys!