union($set2); print_r($union); // Outputs: Ds\Set Object ( [0] => apple [1] => banana [2] => orange [3] => kiwi [4] => pear ) // Intersection $intersection = $set1->intersect($set2); print_r($intersection); // Outputs: Ds\Set Object ( [0] => banana ) // Difference $difference = $set1->diff($set2); print_r($difference); // Outputs: Ds\Set Object ( [0] => apple [1] => orange ) ?>
add('apple'); $set->add('banana'); $set->add('orange'); $set->remove('banana'); $set->add('kiwi'); $set->add('pear'); print_r($set); // Outputs: Ds\Set Object ( [0] => apple [1] => orange [2] => kiwi [3] => pear ) ?>This example creates a set, adds some values to it, removes a value, and adds more values. It uses the same PHP collection library \Ds\Set. In both examples, we used the PHP collection library \Ds\Set to perform set operations and manipulate sets. This library is part of the Data Structures for PHP extension, which provides efficient implementations of common data structures like stacks, queues, heaps, and more.