Backend Development 8 min read

New Array Helper Functions in PHP 8.4 and Their Laravel Equivalents

This article introduces the new array helper functions added in PHP 8.4, demonstrates how to use them with practical examples, and shows equivalent implementations in Laravel using the Arr and Collection classes to simplify array operations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
New Array Helper Functions in PHP 8.4 and Their Laravel Equivalents

Introduction

PHP 8.4 is expected to be released in November 2024, bringing a set of convenient new array functions. This article quickly introduces these functions and shows how to apply them in your PHP 8.4 projects.

Function array_find

The array_find function searches an array for the first element that satisfies a given condition and returns its value, or null if no element matches.

Example: find the product whose barcode is 123456 from a list of products.

$products = [
    [
        'name' => 'Macbook Pro',
        'type' => 'Laptop',
        'barcode' => 123456,
    ],
    [
        'name' => 'Framework Laptop 13',
        'type' => 'Laptop',
        'barcode' => 789012,
    ],
    [
        'name' => 'Samsung Galaxy S24',
        'type' => 'Phone',
        'barcode' => 135791,
    ],
];

// Find product with barcode 123456
$findProduct = array_find(
    array: $products,
    callback: function (array $product): bool {
        return $product['barcode'] == 123456;
    },
);

After execution, $findProduct equals the first product array.

Using an arrow function for brevity:

$findProduct = array_find(
    array: $products,
    callback: fn (array $product): bool => $product['barcode'] === 123456,
);

If no element matches, the function returns null . Example:

$nonExistentProduct = array_find(
    array: $products,
    callback: fn (array $product): bool => $product['barcode'] === 'invalid',
);

In this case $nonExistentProduct is null .

Laravel Equivalent

In Laravel you can achieve the same result with Arr::first :

use Illuminate\Support\Arr;

$findProduct = Arr::first(
    $products,
    fn (array $product): bool => $product['barcode'] === 123456,
);

Function array_find_key

The array_find_key function works like array_find but returns the key of the first matching element instead of its value.

Example: find the key of the product with barcode 789012 :

$findProduct = array_find_key(
    array: $products,
    callback: fn (array $product): bool => $product['barcode'] === 789012,
);

The result is 1 because the matching product is the second element in the array.

Laravel Equivalent

You can combine array_keys with Arr::first :

use Illuminate\Support\Arr;

$firstProductKey = Arr::first(
    array_keys($products),
    fn (int $key): bool => $products[$key]['barcode'] === 789012,
);

Function array_any

The array_any function checks whether at least one element in an array satisfies a given condition, returning true if found, otherwise false .

Example: check if any product is of type Laptop :

$anyProductsAreLaptops = array_any(
    array: $products,
    callback: fn (array $product): bool => $product['type'] === 'Laptop',
);

The variable evaluates to true because there are laptop products.

When no element matches, the function returns false :

$anyProductsAreInvalid = array_any(
    array: $products,
    callback: fn (array $product): bool => $product['type'] === 'Invalid',
);

Here $anyProductsAreInvalid is false .

Laravel Equivalent

Using a collection’s contains method:

use Illuminate\Support\Collection;

$anyProductsAreLaptops = Collection::make($products)->contains(
    fn (array $product): bool => $product['type'] === 'Laptop',
);

Function array_all

The array_all function returns true only if **every** element in the array satisfies the condition; otherwise it returns false .

Example: verify that all products are laptops:

$allProductsAreLaptops = array_all(
    array: $products,
    callback: fn (array $product): bool => $product['type'] === 'Laptop',
);

The result is false because not all products are laptops.

Laravel Equivalent

Using a collection’s every method:

use Illuminate\Support\Collection;

$allProductsAreLaptops = Collection::make($products)->every(
    fn (array $product): bool => $product['type'] === 'Laptop',
);

Conclusion

These new array helper functions in PHP 8.4 provide concise ways to search, filter, and evaluate arrays. Laravel developers can achieve similar functionality with the Illuminate\Support\Arr class and collection methods, enhancing code readability and efficiency.

By incorporating these tools, you gain more options for array manipulation and can write cleaner, more performant PHP code.

backendphpArrayfunctionsLaravelphp8.4
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.