I'm relatively new to php and having a hard time figuring out the right data structure to use. Let's say I have a class FooBar with equals()
and hashCode()
properly implemented. What kind of collection in php (if there is any at all) that most resembles Java's hashSet? I need a collection of objects without duplicates. Someone's suggested using array and the function array_key_exists
, but I was just wondering if there's another way to do this?
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Laravel Option Select - Default Issue
- How to maintain order of key-value in DataFrame sa
Starting from release 5.2 Php offers SplObjectStorage, that offers functionalities of Java's Set:
Check http://technosophos.com/content/set-objects-php-arrays-vs-splobjectstorage for example of use
There are few Data structures available in the PHP programming language provided by the Standard PHP Library (SPL). Although they are nothing when compared with the Java Collections Framework implementations, sometimes they can be very useful by providing a more advanced functionality than that of the arrays. You can find the documentation of the available Data structures here.
The most HashSet-like Data structure in PHP would be SplObjectStorage.
From the documentation:
Objects in PHP don't implement the
equals()
and thehashCode()
methods. Uniqueness of objects is determined by the value returned from thespl_object_hash()
function. The same value is used by theSplObjectStorage
class to uniquely identify the objects it contains. TheSplObjectStorage::getHash($object)
method can be used to retrieve the identifier of an object contained in theSplObjectStorage
collection.