How to implement Java-like set types in PHP

Source: Internet
Author: User
Title, how to implement the set class of Java, in order to store the same object stored only once.

Reply content:

Title, how to implement the set class of Java, in order to store the same object stored only once.

There is no set function in PHP similar to java.
There are objects, arrays, and resources commonly used to store data in PHP.
Storing objects in PHP is usually implemented using arrays.
You just want to store the same object once, just to determine if the array already has the same object before you put the array, and the question turns into how to judge two objects the same .

When comparing two object variables using the contrast operator (= =), the principle of comparison is that if the properties and property values of the two objects are equal, and two objects are instances of the same class, the two object variables are equal.
If you use the strict equality operator (= = =), the two object variables must point to the same instance of a class (that is, the same object).

PHP does not have an existing set-type Object

In simple words, you can use the Array_unique function.

Use the PHP array, each time you insert, with the In_array () function to judge, does not exist insert

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.