Introducing new array functions released in PHP8.4!
table of contents
nice to meet you!
My name is Iosa and I am a first-year member of the system development department!
It's gotten cold all at once recently. Why do I feel lonely when it gets cold?
Well, putting that aside, PHP8.4 has been released! !
At our company, we mainly develop using PHP, but the array functions added this time seemed easy to use, so we would like to introduce them to you!
Added array functions
What was added this time is
- array_find()
- array_find_key()
- array_any()
- array_all()
There will be 4!
All 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
Now let me introduce you! !
array_find function
First up is the array_find function!
the value of the first element for which the callback function returns true null if it is not found .
Below is an example of usage!
$array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Find elements whose array value is 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 , only the first element will be returned.
// *Array values are the same // Find the 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"
As you may have noticed if you are using the PHP framework Laravel, it has the same behavior Arr::first
array_find_key function
Next is the array_find_key function!
the key of the first element whose callback function returns true null if not found .
Below is an example of usage!
$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 are searching elements whose array values are longer than 6 characters, but the returned value is the key of that element
Also, like the array_find function, if the callback function returns true for multiple values, it will only return the key of the first element
// *The array values are the same // Find the 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 the array_search function, However, by using the array_find_key function, you can use conditions and regular expressions that you set in the callback, so I think it has a high degree of freedom and is easy to use in various places!
array_any function
Next is the array_any function!
if there is at least one element for which the callback function returns true it will return
false Below is an example of usage!
$array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Is there an element whose value starts with m? $result = array_any($array, function (string $value){ return str_starts_with($value, 'm'); }); var_dump($result); // Result: bool(false) // Is any value longer than 6 characters? $result = array_any($array, function (string $value){ return strlen($value) > 6; }); var_dump($result ); // Result: bool(true)
behaves the same as
the contains method in Laravel's Collection class (Although the internal processing is different...) For those who have used the contains method, I think it will be easy to imagine the behavior of the array_any function.
array_all function
The last one is the array_all function!
if all the elements for which the callback function true , otherwise it will return false
Below is an example of usage!
$array = [ 'tamabaritake' => 'enoki', 'haratake' => 'shiitake', 'hiratake' => 'eringi', ]; // Do all values start with e? $result = array_all($array , function (string $value){ return str_starts_with($value, 'e'); }); var_dump($result); // Result: bool(false) // Does 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)
has the same behavior as every method in Laravel's Collection class
summary
How was it?
Detailed explanations of the above methods are provided in the official documentation, so be sure to check it out as well!
List of added functions: https://www.php.net/releases/8.4/ja.php#new_array_find
Personally, I felt that the new array functions are versatile and easy to use because they can use callback functions.
I also think that there were many things that were borrowed from or inspired by Laravel.
I would be happy if this trend increases in the future and the convenience of the PHP language itself improves!
I hope you can remember this function and think, ``I wish there was such a function!''