PHP Ds\Set Functions Complete Reference
Last Updated :
25 Jan, 2023
Set is the collection of unique values. The implementation of Ds\Set is similar to the Ds\Map which creates a hash table. The values of Ds\Set are used as key and the mapped values are ignored.
Requirements: PHP 7 is required for both extension and the compatibility polyfill.
Installation: The easiest way to install data structure by using the PECL extension.
pecl install ds
Syntax:
public Ds\Set::functionName()
Example: Below programs illustrate the Ds\Set::count() function:
PHP
<?php
$set = new \Ds\Set([10, 15, 21]);
var_dump( $set );
echo "Count is : " ;
print_r( $set -> count ());
?>
|
Output:
object(Ds\Set)#1 (3) {
[0]=>
int(10)
[1]=>
int(15)
[2]=>
int(21)
}
Count is : 3
Complete list of data structure DS\Set:
Functions
|
Description
|
add() |
It is used to add values to the set. |
allocate() |
Allocate memory for required capacity. |
capacity() |
Return the capacity of the set. |
clear() |
Remove all values from the set. |
__construct() |
Create new instance of set. |
contains() |
Check the given value exists in the set or not. |
copy() |
Return the copy of the set element. |
count() |
Count the number of values present in the Set. |
diff() |
Create a set that contains the elements of the first set which are not present in the second set. |
filter() |
Create new set using filter function. |
first() |
Returns the first element of the Set. |
get() |
Get a value from the Set instance. |
intersect() |
Create a new set that contains the intersection of two sets. |
isEmpty() |
Check whether a set is empty or not. |
join() |
It is used to join all values as string |
last() |
Return the last element from the Set instance. |
merge() |
Returns a set after adding all given values to the set. |
reduce() |
Set to a single value by applying operations using the callback function. |
remove() |
Remove specific values from a Set instance.. |
reverse() |
Reverse the order of elements present in the Set instance. |
reversed() |
Create a copy of the original Set with values arranged in reverse order. |
slice() |
Return the sub-set of a given range. |
sort() |
Sort the elements of a specified Set instance according to the values. |
sorted() |
Return a sorted copy of a given set. |
sum() |
Find the sum of all of the elements present in a Set. |
toArray() |
Convert the Set into an associative array. |
union() |
Create a new set that contains the union of two sets |
xor() |
Create a new set that contains the value either in the first set or second set but not both. |
Share your thoughts in the comments
Please Login to comment...