How to implement set theory operations in pure php?
There are four main set operations which include set union, set intersection, set complement, and set difference.
If there are two sets A and B, then the difference of two sets A and B is equal to the set which consists of elements present in A but not in B. It is represented by A-B. Example: If A = {1,2,3,4,5,6,7} and B = {6,7} are two sets.
The symbol ∪ is employed to denote the union of two sets. Thus, the set A ∪ B—read “A union B” or “the union of A and B”—is defined as the set that consists of all elements belonging to either set A or set B (or both).
Addition of sets A and B, referred to as Minkowski addition, is the set in whose elements are the sum of each possible pair of elements from the 2 sets (that is one element is from set A and the other is from set B). Set subtraction follows the same rule, but with the subtraction operation on the elements.
Which operations are you looking for? What are you trying to accomplish?
In PHP an array is (imperfectly) analogous to a set, and PHP has array_intersect
, array_merge
(union), and array_diff
(complement) functions built in. There's also array_uintersect
and array_udiff
for handling complex objects whose comparisons are not so straightforward.
Is there further functionality you need?
Probably you'll already saw those links:
http://jamietalbot.com/2010/02/04/set-operations-in-php/
http://oreilly.com/catalog/progphp/chapter/ch05.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With