The Set object holds only unique values of any type.
Set() constructorSet::add()Set::clear()Set::delete()Set::difference()Set::entries()Set::forEach()Set::has()Set::intersection()Set::isDisjointFrom()Set::isSubsetOf()Set::isSupersetOf()Set::keys()Set::symmetricDifference()Set::union()Set::values()Set::sizeCreates a new Set object.
$set = new Set(['foo', 'bar', 'foo']);
// ['foo', 'bar']
The add() method adds a new element in to Set.
$set = new Set();
$set->add(40);
// [40]
$set->add('first')->add('second');
// [40, 'first', 'second']
Values are always unique.
$set = new Set();
$set->add(40);
// [40]
$set->add('first')->add('second');
// [40, 'first', 'second']
$set->add(40)->add('second');
// [40, 'first', 'second']
The clear() method removes all elements from the Set.
$set = new Set(['foo', 'bar']);
$set->size;
// 2
$set->clear();
$set->size;
// 0
The delete() method removes a specified value from the Set.
$set = new Set(['foo', 'bar']);
$set->delete('foo');
// true
$set->delete(1);
// false
The difference() method returns a new Set containing elements in this Set but not in the given Set.
$odds = new Set([1, 3, 5, 7, 9]);
$squares = new Set([1, 4, 9]);
$set->difference($squares);
// Set[3, 5, 7]
The entries() method returns a new SetIterator instance that contain pair array [value, value] of each element of the Set.
$set = new Set(['foo', 'bar']);
$iterator = $set->entries();
// [object SetIterator]
$iterator->current();
// ['foo', ''foo']
The forEach() method executes a provided function once per each element of the Set.
$set = new Set([3, 0]);
$set->forEach(function (int $value1, int $value2, Set $set) {
// ...
});
The has() method returns TRUE when element with the specified value exists in the Set or FALSE if it does not exist.
$set = new Set([1, 2, 3, 4, 5]);
$set->has(1);
// true
$set->has(6);
// false
The intersection() method returns a new set containing only those elements that exist in both sets.
$odds = new Set([1, 3, 5, 7, 9]);
$squares = new Set([1, 4, 9]);
$odds->intersection($squares);
// Set[1, 9]
The isDisjointFrom() method checks if Set has no elements in common with the given Set.
$primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
$squares = new Set([1, 4, 9, 16]);
$primes->isDisjointFrom($squares);
// true
The isSubsetOf() method checks if all elements of a Set are in a given Set.
$fours = new Set([4, 8, 12, 16]);
$evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
$fours->isSubsetOf($evens);
// true
The isSupersetOf() method checks if all elements of the given Set are in the Set.
$evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
$fours = new Set([4, 8, 12, 16]);
$evens->isSupersetOf($fours);
// true
The keys() method is exactly equivalent to the values() method.
$set = new Set(['foo' => 'bar']);
$iterator = $set->keys();
// [object SetIterator]
$iterator->current();
// 'foo'
The symmetricDifference() method returns a new Set containing elements that not in both Sets.
$evens = new Set([2, 4, 6, 8]);
$squares = new Set([1, 4, 9]);
$evens->symmetricDifference($squares);
// Set[2, 6, 8, 1, 9 ]
The union() method returns a new Set containing all elements from both Sets.
$odds = new Set([1, 3, 5, 7, 9]);
$squares = new Set([1, 4, 9]);
$odds->union($squares);
// Set[1, 3, 5, 7, 9, 4]
The values() method returns a new SetIterator instance that contain the values of each element of the Set.
$set = new Set(['foo', 'bar']);
$iterator = $set->values();
// [object SetIterator]
$iterator->current();
// 'foo'
$iterator->next();
// 'bar'
The size property returns number of elements from the Set.
$set = new Set(['foo', 'bar']);
$set->size;
// 2
$set->delete('foo');
$set->size;
// 1