首頁  >  文章  >  後端開發  >  PHP 中的新數組函數

PHP 中的新數組函數

王林
王林原創
2024-08-19 18:43:331045瀏覽

New Array Functions in PHP

介紹

PHP 8.4 將於 2024 年 11 月發布,並將引入一些方便的新數組函數:

  • 陣列查找
  • 陣列查找鍵
  • 數組_任意
  • array_all

在本文中,我們將快速瀏覽這些新函數以及如何在執行 PHP 8.4 的專案中使用它們。

如果您是 Laravel 開發人員,您可能會注意到我們在 IlluminateSupportCollection 和 IlluminateSupportArr 類別中已經有了類似的函數。但我喜歡這些函數是 PHP 原生的,因此可以在任何 PHP 專案中使用。

對於任何作為 Laravel 開發人員的讀者,我將向您展示這些新功能的 Laravel 等效項,以便您可以在 Laravel 專案中實現相同的功能,而無需等待 PHP 8.4。

您可能還有興趣查看我的另一篇文章,其中全面介紹了 PHP 8.4 的新「屬性掛鉤」功能。

array_find 函數

array_find 函數傳回與回呼中定義的條件相符的第一個元素的值。如果沒有元素與回調匹配,則函數傳回 null。

讓我們來看一個簡單的例子。我們假設我們有一系列產品,並且我們想要找到條碼為 123456 的產品:

$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 the product with barcode 123456
$findProduct = array_find(
    array: $products,
    callback: function (array $product): bool {
        return $product['barcode'] == 123456;
    },
);

運行上面的程式碼後,$findProduct將等於:

[
    'name'=> 'Macbook Pro',
    'type' => 'Laptop',
    'barcode' => 123456,
]

我們可以透過使用箭頭函數作為第二個參數來進一步清理它:

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

上面的程式碼將傳回與前面的範例相同的結果。

如果沒有元素與回呼匹配,函數將傳回 null。讓我們來看一個例子:

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

在這種情況下,$nonExistentProduct 將等於 null。

Laravel 等效項

在 Laravel 中,您可以使用 Arr::first 方法得到類似的結果:

use Illuminate\Support\Arr;

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

array_find_key 函數

函數與 array_find 函數類似,但它不是傳回與回呼匹配的第一個元素的值,而是傳回與回呼匹配的第一個元素的鍵。

讓我們以先前的 $products 範例陣列為例。這次,我們要找出條碼為 789012 的產品的金鑰:

$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 the key of the product with barcode 789012
$findProduct = array_find_key(
    array: $products,
    callback: fn (array $product): bool => $product['barcode'] === 789012,
);

運行上面的程式碼後,$findProduct 將等於 1,因為產品是陣列中的第二個元素。

如果沒有元素與回呼匹配,函數將傳回 null。讓我們來看一個例子:

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

在這種情況下,$nonExistentProduct 將等於 null。

Laravel 等效項

在 Laravel 中,您可以使用 array_keys 和 Arr::first 方法的組合來實現類似的結果:

use Illuminate\Support\Arr;

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

在上面的程式碼中,我們先使用 array_keys 來取得 $products 陣列的鍵數組。然後我們使用 Arr::first 來尋找與回呼匹配的第一個鍵。這比原生 PHP 函數稍微冗長一些,但它達到了相同的結果。

array_any 函數

array_any 函數可讓您檢查陣列中至少一個元素是否符合回呼中定義的條件。如果任何元素與回調匹配,則函數傳回 true。如果沒有元素與回呼匹配,函數會傳回 false。

繼續使用我們的 $products 範例數組,讓我們檢查是否有任何產品具有筆記型電腦類型:

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

在這種情況下,$anyProductsAreLaptops 將等於 true,因為陣列中至少有一個產品是筆記型電腦。

如果沒有元素與回呼匹配,函數將傳回 false。讓我們來看一個例子:

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

在這種情況下,$anyProductsAreInvalid 將等於 false。

Laravel 等效項

我們可以在 Laravel 中使用集合上的 contains 方法得到相同的結果:

use Illuminate\Support\Collection;

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

在上面的程式碼中,我們從 $products 陣列建立一個集合,然後使用 contains 方法檢查集合中是否有任何產品是筆記型電腦。

array_all 函數

array_all 函數與 array_any 函數類似,但它不是檢查是否至少有一個元素與回調匹配,而是檢查所有元素是否與回調匹配。如果所有元素都與回調匹配,則函數傳回 true。如果任何元素與回呼不匹配,函數將傳回 false。

讓我們檢查 $products 陣列中的所有產品是否都是筆記型電腦:

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

在這種情況下,$allProductsAreLaptops 將等於 false,因為並非陣列中的所有產品都是筆記型電腦。

Laravel Equivalent

In Laravel, we can achieve the same result using the every method on a collection:

use Illuminate\Support\Collection;

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

In the code above, we're creating a collection from the $products array and then using the every method to check if all the products in the collection are laptops.

Conclusion

Hopefully, this article has shown you how you can use the new array functions that will be available in PHP 8.4. It should have also given you an idea of how you can achieve similar functionality in Laravel using the Illuminate\Support\Collection and Illuminate\Support\Arr classes.

If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! ?

以上是PHP 中的新數組函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn