Introducing the new array functions released in PHP 8.4!

Nice to meet you!
I'm Iosa, a first-year employee in the Systems Development Department!

It's gotten really cold recently. Why do I feel lonely when it gets cold?

Anyway, that aside, PHP 8.4 has been released!
Our company mainly develops in PHP, and the array functions added this time look very useful, so I'd like to introduce them to you!

Added array functions

What was added this time was

  • array_find()
  • array_find_key()
  • array_any()
  • array_all()

There are four of them!
All of these functions can be used by passing an array as the first argument and a callback function as the second argument.

The signature of the callback function is as follows, and the first argument can be used as the element value and the second argument as the element key

 callback(mixed $value, mixed $key): bool 

So let's get started with the introduction!!

array_find function

First up is the array_find function,
returns the value of the first element for which the callback function returns true null .

Below is an example of use!

 $array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Find elements whose array values ​​are longer than 6 characters. $result = array_find($array, function (string $value){ return strlen($value) > 6; }); var_dump($result); // Result: string(8) "shiitake"

As shown above, it returns the value that becomes true when strlen($value) > 6 in the callback function

if there are multiple values ​​for which the callback function returns true as shown below , it will only return the first element

 // *The array values ​​are the same // Find an element whose array value starts with e $result = array_find($array, function (string $value){ return str_starts_with($value, 'e'); }); var_dump($result); // Result: string(5) "enoki"

Those of you who use the PHP framework Laravel may have noticed that this behaves the same as the helper function Arr::first (the processing within the function is also almost the same)!

array_find_key function

Next up is the array_find_key function!

the key of the first element for which the callback function returns true null if not found .
Here is an example of its use!

 $array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Find array values ​​longer than 6 characters. $result = array_find_key($array, function (string $value){ return strlen($value) > 6; }); var_dump($result); // Result: string(8) "haratake"

As shown above, we searched for an element whose array value is longer than 6 characters, and the returned value is the key of that element

Also, like the array_find function, if there are multiple values ​​for which the callback function returns true, it will only return the key of the first element

 // *The array values ​​are the same // Search for an element whose array value starts with e $result = array_find_key($array, function (string $value){ return str_starts_with($value, 'e'); }); var_dump($result); // Result: string(12) "tamabaritake" 

, there was a
similar function called array_search However, by using array_find_key, you can use your own conditions and regular expressions within the callback, giving you a high degree of flexibility and making it easy to use in a variety of places!

array_any function

Next is the array_any function!
if there is at least one element for which the callback function true false if there is none .
Here is an example of how to use it!

$array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Are there any elements where any of the values ​​start with m? $result = array_any($array, function (string $value){ return str_starts_with($value, 'm'); }); var_dump($result); // Result: bool(false) // Are there any values ​​longer than 6 characters? $result = array_any($array, function (string $value){ return strlen($value) > 6; }); var_dump($result); // Result: bool(true)

works the same as
the contains method in Laravel's Collection class For those who have used the contains method, it should be easy to imagine how the array_any function works.

array_all function

Finally, there is the array_all function!
if all elements in the array have a callback function that returns true false if none

Below is an example of use!

$array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // All values ​​start with e $result = array_all($array, function (string $value){ return str_starts_with($value, 'e'); }); var_dump($result); // Result: bool(false) // All array keys end with take $result = array_any($array, function (string $value, string $key){ return preg_match('/.*take$/', $key); }); var_dump($result); // Result: bool(true)

behaves the same as the every method in Laravel's Collection class

summary

What did you think?

A detailed explanation of the above methods is provided in the official documentation, so please check that out as well!
List of added functions: https://www.php.net/releases/8.4/ja.php#new_array_find

Personally, I got the impression that the new array functions are versatile and easy to use because they allow the use of callback functions

I also think that many of the features were borrowed or inspired by Laravel.
I hope that this trend will continue to grow and that the convenience of the PHP language itself will improve!

I hope you'll just remember that this function exists!

 

 

If you found this article useful, please click [Like]!
5
Loading...
5 votes, average: 1.00 / 15
677
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Iosa

It's basically on a bike