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!
the callback function returns truethe value of the first element for whichreturnsnullreturns

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

as shown belowif there are multiple values ​​for which the callback function returns true,only the first elementwill 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!

of the first element for which the callback function returns truethe keyreturnsnullreturns
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 forelements in the array whose values​​are longer than 6 characters, and the returned valuethe key of that elementis

Also, similar to the array_find function, if there are multiple values ​​for which the callback function returns true,the key of the first elementit will only return

 // *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" 

a similar function`array_search`, there was
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!
element whose callback functionat least oneif there is`true`otherwisefalse`returns
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)

in Laravel's `Collection` classthe `contains` methodbehaves the same way as
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!
the elements that the callback function returns trueallifareotherwisefalse.

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)

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

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
775
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Iosa

It's basically on a bike