Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Built in support for sets in PHP?

Tags:

php

set

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.

like image 517
devios1 Avatar asked Nov 14 '11 20:11

devios1


People also ask

Are there PHP sets?

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.

What is set () in PHP?

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.

Does PHP have a stack?

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.


1 Answers

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; 
like image 156
zrvan Avatar answered Sep 22 '22 15:09

zrvan