Introducing the new array functions released in PHP 8.4!

Nice to meet you!
I'm Iosa from the Systems Development Department, and I'm in my first year as a working professional!

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

Anyway, putting that aside, PHP 8.4 has been released!
Our company primarily uses PHP for development, and I wanted to introduce the new array functions that seem quite useful!

Added array functions

What was added this time was

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

These are the four functions!
All of them 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! This function returns the value of the first element for which the callback function returns true , and returns null if it is not found

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 for which strlen($value) > 6 is true within the callback function

Furthermore, if there are multiple values ​​for which the callback function returns true , as shown below , only the first element will be returned.

 // *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 using the PHP framework Laravel may have already noticed, but it behaves the same as the helper functionArr::first(the processing within the function is almost identical)!

array_find_key function

Next up is the array_find_key function!

This function returns the key of the first element for which the callback function returns true , and returns null if it is not found. Here is an example of how to use it!

 $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 described above, we searched for elements in the array whose values ​​are longer than 6 characters, and the returned value is the key of that element

Also, similar to 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" 

Previously , there was a similar function called `array_search` that searched for specific values ​​and returned the corresponding key. However, using the `array_find_key` function allows you to use conditions and regular expressions that you set yourself within the callback, making it more flexible and easier to use in various situations!

array_any function

Next up is the `array_any` function! This function returns `true` if there is at least one element whose callback function returns `true` , and ` false` otherwise . Here's 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)

This also behaves the same way as the `contains` method in Laravel's `Collection` class ! (Although the internal processing is different...) If you've used the `contains` method, you should be able to easily imagine how the `array_any` function works.

array_all function

Finally, there's the array_all function! This function returns true if all the elements that the callback function returns true are present, and false otherwise .

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)

This function behaves similarly to the `array_any` function, and is the same as the `every` method found in Laravel's Collection class

summary

What did you think?

Detailed explanations of the above methods can be found in the official documentation, so please be sure to 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

Also, I think there were many things that were reused or inspired by Laravel.
I hope this trend continues and that the usability of the PHP language itself improves!

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

 

 

If you found this article helpful,please give it a "Like"!
5
Loading...
5 votes, average: 1.00 / 15
833
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Iosa

It's basically on a bike