What is the Ds\Set::clear method in php?

The clear method of the Set data structure removes all the Set values.

A Set is a collection of unique values. In Ds\Set, we can store any value, including objects. The insertion order of the element is preserved.

Installing DS

By default, Data Structures (DS) is not installed in PHP. We can install it by following these instructions on php.net.

Syntax

This method will make the Set empty.


public Ds\Set::clear(): void

Code

<?php
$set = new \Ds\Set();
$set->add(1,2,3);
print_r($set); //Ds\Set Object ( [0] => 1 [1] => 2 [2] => 3 )
$set->clear();
print_r($set); //Ds\Set Object ( )
?>

Explanation

In the code above:

  • We have created an object of type Set data structure and added elements using the add method.

  • Then used the clear method of the Set. This will remove all the elements of the Set.

Free Resources