All posts
Published in PHP

PHP 8.4: New Array Functions

Profile image of Atakan Demircioğlu
By Atakan Demircioğlu
Fullstack Developer

PHP 8.4 introduces four new array functions that make use of callback functions for searching and inspecting array elements: array_find, array_find_key, array_all, and array_any.

These functions simplify common operations such as finding array elements that match specific criteria (checked using a callback function) and verifying if any or all elements in an array meet a certain condition.

The array_find Function

The array_find function returns the value of the first array element for which the callback returns true. If no elements match the condition, the function returns null.

Function Synopsis

/**
 * Returns the VALUE of the first element from $array for which the
 *  $callback returns true. Returns null if no matching element is
 *  found.
 *
 * @param array $array The array to be searched.
 * @param callable $callback The callback function to check
 *  each element. The first parameter contains the value ($value),
 *  the second parameter contains the corresponding key ($key).
 *  If this callback returns true (or a truthy value), the value
 *  ($value) is returned immediately, and the callback will not be
 *  called for further elements.
 *
 * @return mixed The function returns the value of the first
 *  element for which the $callback returns true. null, if no
 *  matching element is found. Note that the matching element value
 *  itself could be null as well.
 */
function array_find(array $array, callable $callback): mixed {}

Usage Examples

function is_even(int $value): bool {
    return $value % 2 === 0;
}
echo array_find([1, 2, 3, 4, 5], 'is_even'); // Output: 2

echo array_find([1, 2, 3, 4, 5], fn($value) => $value % 2 === 0); // Output: 2

function is_key_numeric(mixed $value, mixed $key): bool {
    return is_numeric($key);
}
echo array_find(['a' => 'foo', 2 => 'bar'], 'is_key_numeric'); // Output: "bar"

The array_find_key Function

The array_find_key function is similar to array_find, but it returns the key of the first array element for which the callback returns true. If no elements match the condition, the function returns null.

Function Synopsis

/**
 * Returns the KEY of the first element from $array for which the
 *  $callback returns true. If no matching element is found, the
 *  function returns null.
 *
 * @param array $array The array to be searched.
 * @param callable $callback The callback function to check
 *  each element. The first parameter contains the value ($value),
 *  the second parameter contains the corresponding key ($key). If
 *  this function returns true, the key ($key) is returned
 *  immediately, and the callback will not be called for further
 *  elements.
 *
 * @return mixed The key of the first element for which the
 *  $callback returns true. null, if no matching element is found.
 */
function array_find_key(array $array, callable $callback): mixed {}

Usage Examples

function is_even(int $value): bool {
    return $value % 2 === 0;
}
echo array_find_key(['foo' => 1, 'bar' => 2, 'baz' => 3], 'is_even'); // Output: "bar"

echo array_find_key(
    ['foo' => 1, 'bar' => 2, 'baz' => 3],
    fn($value) => $value % 2 === 0
); // Output: "bar"

function is_key_numeric(mixed $value, mixed $key): bool {
    return is_numeric($key);
}
echo array_find_key(['a' => 'foo', 2 => 'bar'], 'is_key_numeric'); // Output: "2"

The array_all Function

The array_all function takes an array and a callback, and returns true if all elements in the array return true when passed to the callback. The callback function is called with the value ($value) and key ($key) of each element in the array. array_all returns true for empty arrays ([]).

Function Synopsis

/**
 * Checks whether the $callback returns true for all the array
 *  elements.
 *
 * @param array $array The array to be searched.
 * @param callable $callback The callback function to check
 *  each element. The first parameter contains the value ($value), the
 *  second parameter contains the corresponding key ($key). If this function
 *  returns false (or any falsy value), false is returned immediately,
 *  and the $callback will not be called for further elements.
 *
 * @return bool true, if $callback returns true for all elements.
 *  false otherwise.
 */
function array_all(array $array, callable $callback): bool {}

Usage Examples

echo array_all(
    ['foo@example.com', 'bar@example.com', 'baz@example.com'],
    fn($value) => filter_var($value, FILTER_VALIDATE_EMAIL)
); // Output: true

echo array_all(
    ['foo@example.com', 'bar@example.com', 'baz'],
    fn($value) => filter_var($value, FILTER_VALIDATE_EMAIL)
); // Output: false

echo array_all(
    [1 => '', 2 => '', 3 => ''],
    fn($value, $key) => is_numeric($key)
); // Output: true

The array_any Function

The array_any function is similar to array_all. It takes an array and a callback, and returns true if any element in the array returns true when passed to the callback. The callback function is called with the value ($value) and key ($key) of each element in the array. Once the first element returns true from the callback, the callback is not called on the remaining elements. array_any returns false for empty arrays ([]).

Function Synopsis

 * Checks whether the $callback returns true for any of the array
 *  elements.
 *
 * @param array $array The array to be searched.
 * @param callable $callback The callback function to check
 *  each element. The first parameter contains the value ($value), the
 *  second parameter contains the corresponding key ($key). If this
 *  function returns true (or a truthy value), true is returned
 *  immediately, and the $callback will not be called for further
 *  elements.
 *
 * @return bool true if there is at least one element for which
 *  $callback returns true. false otherwise.
 */
function array_any(array $array, callable $callback): bool {}

Usage Examples

echo array_any(
    ['foo@example.com', 'https://keeplearning.dev', 'foobar'],
    fn($value) => filter_var($value, FILTER_VALIDATE_URL)
); // Output: true

echo array_all(
    ['https://keeplearning.dev', new stdClass()],
    fn($value) => filter_var($value, FILTER_VALIDATE_EMAIL)
); // Output: false

echo array_any(
    [1 => '', 'bar' => '', 'baz' => ''],
    fn($value, $key) => is_numeric($key)
); // Output: true