tags:

views:

49

answers:

1

How do I write code such that I can put things in a set and only the unique entries are kept?

for($i=0;$i<$count;$i++) {
  $variable->put($some_object)
}
+6  A: 

SplObjectStorage

$s = new SplObjectStorage();
$s->attach($some_object);
$s->attach($some_other_object);

Note that you can also use arrays, the keys are unique and reassigning a new value to existing key overwrites the new one. But with arrays you have come up with your own ID-s, with SplObjectStorage you don't.

Array example

$a = array();
$a['key1'] = $some_object;
$a['key1'] = $some_other_object;

In the above example only 'key1' is kept.

Anti Veeranna
Doesn't work with strings
erotsppa
Very true, SplObjectStorage does not work with plain old strings, you would have to wrap them somehow. Or just us an array for strings :)
Anti Veeranna