Built in Support for Sets in PHP

Built in support for sets in PHP?

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;

Is there the equivent of a Java Set in php?

You could just use an array and put the data you want in the key because keys can't be duplicated.

Does PHP have built-in data structures?

The only native data structure in PHP is array. Fortunately, arrays are quite flexible and can be used as hash tables as well.

http://www.php.net/array

However, there is SPL which is sort of a clone of C++ STL.

http://www.php.net/manual/en/book.spl.php

Does PHP have a Set data structure?

Yes, you could use the array object and the array functions.

Basically, you could dynamically grow an array using the array_push function, or the $arrayName[] = ... notation (where arrayName is the name of your array).



Related Topics



Leave a reply



Submit