I'm looking for a simple way to create an array in php that will not allow duplicate entries, but allows for easy combining of other sets or arrays.
I'm mostly interested in whether such a feature exists in the language because writing my own wouldn't be difficult. I just don't want to if I don't need to.
The answer is no, there is not a native set solution inside PHP. There is a Set data structure, but that is not baseline PHP. There is a convention for implementing sets using maps (i.e. associative arrays) in any language.
In PHP __set() is used when a value is to be assigned to an undefined variable of a class and __get() is used when the value from an undefined variable is to be read. The __set() is run when writing data to inaccessible properties.
The php stack is defined as the class that will be used for storing the elements in the stack memory. The stack is the sequential set of collections based on a particular property.
Just an idea, if you use the array keys instead of values, you'll be sure there are no duplicates, also this allows for easy merging of two "sets".
$set1 = array ('a' => 1, 'b' => 1, ); $set2 = array ('b' => 1, 'c' => 1, ); $union = $set1 + $set2;
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