## 组 > 将包含基本类型的PHP数组包装到数学集的类。 ### 创建 要创建Set,请仅使用包含基元的平面数组: ``` use \Phpml\Math\Set; $set = new Set([1, 2, 2, 3, 1.1, -1, -10]); $set->toArray(); // return [-10, -1, 1, 1.1, 2, 3] $set = new Set(['B', '', 'A']); $set->toArray(); // return ['', 'A', 'B'] ``` 注入的数组按`SORT_ASC`排序,删除重复项并重写索引 ***** ### 并集 创建两个集合的并集: ``` use \Phpml\Math\Set; $union = Set::union(new Set([1, 3]), new Set([1, 2])); $union->toArray(); //return [1, 2, 3] ``` ***** ### 交集 创建两个集合的交集: ``` use \Phpml\Math\Set; $intersection = Set::intersection(new Set(['A', 'C']), new Set(['B', 'C'])); $intersection->toArray(); //return ['C'] ``` ***** ### 补充 创建两个集的集合论差异: ``` use \Phpml\Math\Set; $difference = Set::difference(new Set(['A', 'B', 'C']), new Set(['A'])); $union->toArray(); //return ['B', 'C'] ``` ***** ### 添加元素 ``` use \Phpml\Math\Set; $set = new Set([1, 2]); $set->addAll([3]); $set->add(4); $set->toArray(); //return [1, 2, 3, 4] ``` ***** ### 删除元素 ``` use \Phpml\Math\Set; $set = new Set([1, 2]); $set->removeAll([2]); $set->remove(1); $set->toArray(); //return [] ``` ***** ### 检查成员 ``` use \Phpml\Math\Set; $set = new Set([1, 2]); $set->containsAll([2, 3]); //return false $set->contains(1); //return true ``` ***** ### 基数 ``` use \Phpml\Math\Set; $set = new Set([1, 2]); $set->cardinality(); //return 2 ``` ***** ### 是否为空 ``` use \Phpml\Math\Set; $set = new Set(); $set->isEmpty(); //return true ``` ***** ### 使用循环 ``` use \Phpml\Math\Set; $set = new Set(['A', 'B', 'C']); foreach($set as $element) { echo "$element, "; } // echoes A, B, C ```