• PHP Video Tutorials

PHP - Set Functions



A set is a collection of distinctive values. It utilizes the same hash table as Ds Map. The mapped value is not taken into account in this implementation. The set accepts the value of any type including objects.

The array syntax, which is represented in square brackets is supported by set. The order of insertion is maintained. It automatically releases reserved memory when it reaches a certain size. The add(), remove() and contains() are all executed in O(1).

The set data structure in PHP doesn’t support push(), pop(), insert(), shift(), or unshift().

Strengths

  • Values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Automatically frees allocated memory when its size drops low enough.
  • add(), remove() and contains() are all O(1).

Weaknesses

  • Doesn’t support push(), pop(), insert(), shift(), or unshift().
  • get() is O(n) if there are deleted values in the buffer before the accessed index, O(1) otherwise.

List of Functions

Following are the list of functions provided by the Set class −

Sr.No. Functions & Description
1

Ds\Set::add()

This Function can can add values to the set.

2

Ds\Set::allocate()

This Function can allocate enough memory for the required capacity.

3

Ds\Set::capacity()

This Function can return the current capacity.

4

Ds\Set::clear()

This Function can remove the all values.

5

Ds\Set::__construct()

This Function can create a new instance.

6

Ds\Set::contains()

This Function can determine if the set contains all values.

7

Ds\Set::copy()

This Function can return a shallow copy of the set.

8

Ds\Set::count

This Function can be used to count the number of values present in a set, and also referred to as the size of a set instance.

9

Ds\Set::diff()

This Function can create a new set by using the values that aren't in another set.

10

Ds\Sequence::filter()

This Function can create the new set by using a callable to determine which values to include.

11

Ds\Set::first()

This Function can return the first value in a set.

12

Ds\Set::get()

This Function can return the value at a given index.

13

Ds\Set::intersect()

This Function can create a new set by intersecting the values with another set.

14

Ds\Set::isEmpty()

This Function can return whether the set is empty.

15

Ds\Set::join()

This Function can join all values together as a string.

16

Ds\Set::jsonSerialize()

This Function can return a representation that can be converted to JSON.

17

Ds\Set::last()

This Function can return the last value in a set.

18

Ds\Set::merge()

This Function can return the result of adding all given values to a set.

19

Ds\Set::reduce()

This Function can reduce a set to single value by using callback function.

20

Ds\Set::remove()

This Function can remove all given values from the set.

21

Ds\Set::reverse()

This Function can can reverse a set in-place.

22

Ds\Set::reversed()

This Function can return a reversed copy.

23

Ds\Set::slice()

This Function can return the sub-set of a given range.

24

Ds\Set::sort()

This Function can return a sorted copy.

25

Ds\Set::sorted()

This Function can return a sorted copy.

26

Ds\Set::sum()

This Function can return the sum of all values in a set.

27

Ds\Set::toArray()

This Function can convert a set to an array.

28

Ds\Set::union()

This Function can create a new set by using values from the current instance and another set.

29

Ds\Set::xor()

This Function can create a new set by using the values in the current instance and another set but not in both.

php_function_reference.htm
Advertisements