PHP 8.4 has new Array Find Functions

Chriscullis
2 min readJul 10, 2024

--

Photo by Luca Bravo on Unsplash

PHP 8.4 has introduced some exciting new features to enhance the functionality and ease of working with arrays. Among these, the new array find functions stand out as powerful tools for developers. Let’s dive into these new additions and see how they can be leveraged in your PHP projects.

1. `array_find()`

The `array_find()` function is designed to search through an array and return the first element that matches a given condition. This function is particularly useful when you need to locate a specific item in an array based on custom logic.

Syntax:

array_find(array $array, callable $callback): mixed

  • $array: The array to search.
  • $callback: A callback function that defines the condition. It should return `true` for the element you want to find.

Example:

$numbers = [1, 2, 3, 4, 5];

$result = array_find($numbers, function($value) {
return $value > 3;
});

echo $result; // Outputs: 4

In this example, `array_find()` iterates through `$numbers` and returns the first value greater than 3.

2. `array_find_key()`

The `array_find_key()` function searches an array and returns the key of the first element that matches the specified condition. This function is useful when you need to know the position of an element in the array.

Syntax:

array_find_key(array $array, callable $callback): int|string|null

  • $array: The array to search.
  • $callback: A callback function that defines the condition. It should return `true` for the element whose key you want to find.

Example:

$users = [
‘john’ => [‘age’ => 25],
‘jane’ => [‘age’ => 30],
‘doe’ => [‘age’ => 35],
];

$key = array_find_key($users, function($user) {
return $user[‘age’] > 28;
});

echo $key; // Outputs: jane

In this example, `array_find_key()` returns the key (`jane`) of the first user whose age is greater than 28.

3. `array_find_index()`

The `array_find_index()` function is similar to `array_find_key()`, but it returns the numerical index of the first element that matches the given condition. This is particularly useful for indexed arrays.

Syntax:

array_find_index(array $array, callable $callback): int|null

  • $array: The array to search.
  • $callback: A callback function that defines the condition. It should return `true` for the element whose index you want to find.

Example:

$colors = [‘red’, ‘green’, ‘blue’, ‘yellow’];

$index = array_find_index($colors, function($color) {
return $color === ‘blue’;
});

echo $index; // Outputs: 2

In this example, `array_find_index()` returns the index (`2`) of the color `blue`.

Conclusion

The new array find functions in PHP 8.4 provide a more efficient and expressive way to search arrays based on custom conditions. By using `array_find()`, `array_find_key()`, and `array_find_index()`, developers can write cleaner and more intuitive code when working with arrays. These functions help to simplify array searches and improve overall code readability, making PHP an even more powerful tool for web development.

With these new additions, PHP continues to evolve, offering developers the tools they need to build robust and efficient applications.

--

--