查詢

Ds\Set::union()函式—用法及示例

「 返回當前集合與其他一個或多個集合的並集 」


函式名:Ds\Set::union()

適用版本:PHP 7.3.0 及以上版本

用法:Ds\Set::union() 函式用於返回當前集合與其他一個或多個集合的並集。

語法:public Ds\Set Ds\Set::union ( mixed $...values )

引數:

  • $values: 需要與當前集合求並集的一個或多個集合。可以是可迭代物件、陣列、或單個集合。

返回值: 返回一個新的 Ds\Set 物件,包含當前集合與其他集合的並集。

示例:

$set1 = new Ds\Set([1, 2, 3]);
$set2 = new Ds\Set([2, 3, 4]);
$set3 = new Ds\Set([3, 4, 5]);

$unionSet = $set1->union($set2, $set3);

var_dump($unionSet->toArray());

輸出:

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}

在上面的示例中,我們首先建立了三個不同的 Ds\Set 集合 $set1、$set2 和 $set3。然後使用 union() 方法將這三個集合求並集,返回一個新的集合 $unionSet。最後,我們透過將 $unionSet 轉換為陣列,使用 toArray() 方法輸出結果。

注意:Ds\Set::union() 是一個不修改原始集合的方法,它返回一個新的集合作為結果並集。

補充糾錯
上一個函式: Ds\Set::toArray()函式
下一個函式: Ds\Set::xor()函式
熱門PHP函式
分享連結