How to implement a set type similar to java in PHP

Source: Internet
Author: User
For example, how to implement the java set class is to store the same object only once. For example, how to implement the java set class is to store the same object only once.

Reply content:

For example, how to implement the java set class is to store the same object only once.

PHP does not have the set function similar to java.
Objects, arrays, and resources are commonly used to store data in PHP.
Objects stored in PHP are usually implemented using arrays.
You only want to store the same object once. You only need to judge whether the same object already exists in the array before placing it, so the problem is convertedHow to determine if two objects are the same.

When two object variables are compared using the comparison operator (=), the comparison principle is: if the attributes and attribute values of the two objects are equal, and the two objects are instances of the same class, the two object variables are equal.
If the full equality operator (=) is used, these two object variables must point to the same instance of a class (that is, the same object ).

PHP does not have a ready-made set object.

You can use the array_unique function in a simple way.

Use the php array, and use the in_array () function to determine each insert operation. If no input exists, insert the data.

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.