The Wayback Machine - https://web.archive.org/web/20240809213526/https://www.geeksforgeeks.org/php-dsset-functions-complete-reference/
Open In App

PHP Ds\Set Functions Complete Reference

Last Updated : 25 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
 
// Declare new Set
$set = new \Ds\Set([10, 15, 21]);
 
// Display the Set element
var_dump($set);
 
// Display count of elements
// present in the 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.

Previous Article
Next Article

Similar Reads

PHP Calendar Functions Complete Reference
The calendar extension contains a series of functions to represent the calendar date into a different format. The standard date is based on Julian Day Count. The date count starting from January 1st, 4713 B.C. is known as Julian Day Count. First, the date is converted into Julian Day Count and then converted into a calendar system. Example: The pro
2 min read
PHP Gmagick Functions Complete Reference
Gmagick is the PHP extension used to create, modify and obtain meta information of an image using GraphicsMagick API. The Gmagick consists of Gmagick, GmagickDraw, and GmagickPixel class. Example: Below programs illustrate the Gmagick::getpackagename()Function in PHP: Original Image: C/C++ Code &lt;?php // Create new Gmagick object $im = new Gmagic
3 min read
PHP Ds\Deque Functions Complete Reference
A deque data structure is used to store the sequence of values in a contiguous memory buffer which grows and shrinks automatically. The deque is the abbreviation of "double-ended queue". Requirements: PHP 7 is required for both extension and the compatibility polyfill. Installation: The easiest way to install data structure by using the PECL extens
3 min read
PHP Ds\Map Functions Complete Reference
A Map is a sequential collection of key-value pair which is very similar to the array. The key of a map can be of any type and it is unique. If any value added to the same key in a Map then the map value will replace. It is the efficient Data Structure in PHP 7 to provide the alternative of an array. Requirements: PHP 7 is required for both extensi
3 min read
PHP Filesystem Functions Complete Reference
The Filesystem function is used to access and manipulate filesystem. It is the part of PHP code so no need to install these functions. For accessing the files on the system, the file path will be used. On Unix system, forward slash (/) is used as a directory separator and on Windows platform, both forward slash (/) and backward slash (\) will be us
4 min read
PHP Image Processing and GD Functions Complete Reference
Image processing and GD functions are used to create and manipulate image files in different image formats including GIF, PNG, JPEG, WBMP, and XPM. PHP can deliver the output image directly to the browser. The image processing and GD functions are used to compile the image functions for this work. It can also require other libraries, depending on t
3 min read
PHP GMP Functions Complete Reference
The GMP function uses arbitrary-length integers to perform mathematical operations. The GMP function contains the GMP number as an argument. Example: Program to perform the division of GMP numbers when numeric strings as GMP numbers are passed as arguments. C/C++ Code &lt;?php // PHP program to perform division of // GMP numbers passed as arguments
3 min read
PHP String Functions Complete Reference
Strings are a collection of characters. For example, 'G' is the character and 'GeeksforGeeks' is the string. Installation: These functions are not required any installation. These are the part of PHP core. The complete list of PHP string functions are given below: Example: This program helps us to calculate a 32-bit CRC for the string “Hello World”
6 min read
PHP Math Functions Complete Reference
The predefined math functions in PHP are used to handle the mathematical operations within the integer and float types. These math functions are part of the PHP core. Installation: These functions have not required any installation. The complete list of PHP math functions are given below: Example: Programs illustrate the decbin() function in PHP: C
3 min read
PHP Imagick Functions Complete Reference
The Imagick Function is used to create and modify images using the ImageMagick API. ImageMagick is the software suite to create edit and modify the composing bitmap images. Thisread, writes and converts images in many formats including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Requirements: PHP 5.1.3 and ImageMag
6 min read
three90RightbarBannerImg