PHP - Ds Set count() 函式



PHP 的 Ds\Set::count() 函式用於檢索當前集合中存在的元素數量,也稱為集合例項的大小。

此函式類似於其他程式語言中的 size()length() 方法。

語法

以下是 PHP Ds\Set::count() 函式的語法:

public int Ds\Set::count()

引數

此函式不接受任何引數。

返回值

此函式返回當前集合中存在的元素數量。

示例 1

以下是 PHP Ds\Set::count() 函式的基本示例。

<?php
   $set = new \Ds\Set([1, 2, 3]);
   echo "The set elements are: \n";
   print_r($set);
   echo "The number of elements of set: ";
   print_r($set->count());
?>

輸出

以上程式產生以下輸出:

The set elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The number of elements of set: 3

示例 2

以下是 PHP Ds\Set::count() 函式的另一個示例。我們使用此函式計算此集合中存在的元素數量。

<?php
   $set = new \Ds\Set(["a", "e", "i", "o", "u"]);
   echo "The elements are: \n";
   print_r($set);
   echo "The number of elements: ";
   print_r($set->count());
?>

輸出

執行以上示例後,將顯示以下輸出:

The elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The number of elements: 5
php_function_reference.htm
廣告