-1

Basically what I need is to push a large set of strings to SplObjectStorage and if each string doesn't exist do some kind of action, if it does exist do other action.

$o1->id = '11111';
$s->attach($o1);
$o1->id = '22222';
$s->attach($o1);

I only get 22222 in the object, or it overwrites the object. I need to get both, And if they match just one of them. I need to get distinct values of my strings

user3410843
  • 195
  • 3
  • 18
  • 1
    You only have one object, `$o1`, so you're overwriting the values of that object. Create a new object (`$o2`) and add that to the storage. – nickb Apr 29 '14 at 15:03
  • From what you posted there is neither a reason to use objects in general or `SplObjectStorage` in particular. A simple array with string keys could do the same. – Yoshi Apr 29 '14 at 15:10
  • I have 47000 strings and I need some efficiency, and I am stuck what to you use, I thought that it will speed up the process – user3410843 Apr 29 '14 at 15:22
  • 1
    Sounds like a [x/y-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. I think you should update your question and ask about your real problem. – Yoshi Apr 29 '14 at 15:26

1 Answers1

0

The problem here is that you're updating the same object, and the object itself is not added twice.

If you're only working with strings, it would be more efficient to use an array:

$array = [];

if (!isset($array['11111'])) {
    $array['11111'] = true;
}
// etc.

Afterwards, you can use array_keys() to fetch all unique values.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309