查詢

Ds\Collection::toArray()函式—用法及示例

「 將 Ds\Collection 物件轉換為普通的 PHP 陣列 」


函式名: toArray()

函式說明: toArray() 函式用於將 Ds\Collection 物件轉換為普通的 PHP 陣列。

使用版本: PHP >= 7.4

語法: public Ds\Collection::toArray(): array

引數: 無

返回值: 返回一個包含 Ds\Collection 物件中所有元素的陣列。

示例:

// 建立一個 Ds\Collection 物件
$collection = new Ds\Collection([1, 2, 3, 4, 5]);

// 使用 toArray() 函式將 Ds\Collection 物件轉換為陣列
$array = $collection->toArray();

// 列印陣列
print_r($array);

輸出結果:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

注意事項:

  1. toArray() 函式僅適用於 Ds\Collection 型別的物件,無法對其他型別的物件或陣列進行轉換。
  2. 在 PHP 7.4 以下的版本中不支援該函式,必須使用 PHP 7.4 及以上版本。
  3. 可以使用該函式將 Ds\Collection 物件轉換為普通陣列,便於進行其他操作和處理。
補充糾錯
上一個函式: Ds\Collection::isEmpty()函式
下一個函式: Ds\Deque::allocate()函式
熱門PHP函式
分享連結