Is there an equivalent Java set in php?

Is there an equivalent Java set in php?

(which means a collection that cannot contain the same element twice)

+5
source share
3 answers

You can simply use the array and put the necessary data into the key, because the keys cannot be duplicated.

+10
source

You can use a standard array of PHP values ​​and pass it through array_unique :

$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);

Outputs:

array(2) {
  [0] => int(4)
  [2] => string(1) "3"
}
+5
source

SplObjectStorage - .

$storage = new SplObjectStorage;
$obj1    = new StdClass;

$storage->attach($obj1);
$storage->attach($obj1); // not attached
echo $storage->count();  // 1

$obj2    = new StdClass; // different instance
$obj3    = clone($obj2); // different instance

$storage->attach($obj2);
$storage->attach($obj3);    
echo $storage->count();  // 3

, . , , Spl Data Structures ArrayObject Array.

+4

All Articles