Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

A few new array functions are making their way into PHP 8.4

PHP 8.4 is still under development and has a bunch of new features and improvements under its belt so far. New features keep on appearing and one of them recently surfaced is the addition of new array methods.

There’s this RFC (still under voting phase) which proposes to add these new methods which are essentially related to checking an array for the existence of elements matching a specific condition.

The new methods are:

  • array_find
  • array_find_key
  • array_any
  • array_all

Let’s take a look at each of them.

The array_find method

The array_find returns the value of the first element for which the $callback returns true. If no matching element is found the function returns NULL.

array_find(array $array, callable $callback)

Here’s an example.

$fruits = [
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange',
];

var_dump(array_find($fruits, function ($fruit) {
    return str_starts_with($fruit, 'y')
}));
// Output: string(5) "yellow"

var_dump(array_find($fruits, function ($fruit) {
    return str_starts_with($fruit, 'z')
}));
// Output: NULL

The array_find_key method

The array_find_key returns the key of the first element for which the $callback returns true.

array_find_key(array $array, callable $callback)

Here’s an example.

$fruits = [
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange',
];

var_dump(array_find_key($fruits, function ($fruit) {
    return str_starts_with($fruit, 'y')
}));
// Output: string(5) "banana"

var_dump(array_find_key($fruits, function ($fruit) {
    return str_starts_with($fruit, 'z')
}));
// Output: NULL

The array_any method

The array_any method returns true if any of the elements in the array pass the $callback test. Otherwise, it returns false.

array_any(array $array, callable $callback)

This method resembles the Array.prototype.some() method in JavaScript.

Here’s an example.

$fruits = [
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange',
];

var_dump(array_any($fruits, function ($fruit) {
    return str_starts_with($fruit, 'y')
}));
// Output: true

var_dump(array_any($fruits, function ($fruit) {
    return str_starts_with($fruit, 'z')
}));
// Output: false

The array_all method

The array_all method returns true if all of the elements in the array pass the $callback test. Otherwise, it returns false.

array_all(array $array, callable $callback)

Here’s an example.

$fruits = [
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange',
];

var_dump(array_all($fruits, function ($fruit) {
    return strlen($fruit) > 2
}));
// Output: true

var_dump(array_all($fruits, function ($fruit) {
    return str_starts_with($fruit, 'z')
}));
// Output: false

In closing

While the RFC is still in voting phase, I believe these methods will end up being part of PHP 8.4 looking at the voting trends since most of the votes are in favor of adding these methods at this point.

These array methods are truly nice to have and let us write more concise and readable code.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?