array_unique的对象? [英] array_unique for objects?

查看:141
本文介绍了array_unique的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有像array_unique为对象的任何方法?我有一大堆与角色阵列的对象,我合并,然后我要带出重复:)

Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :)

推荐答案

array_unique() 比较元素的字符串值:

Well array_unique() compares the string value of the elements:

注:有两个因素被认为是平等的,当且仅当(字符串)$ elem1 ===(字符串)$ elem2时。在话:当字符串重新presentation是一样的。第一元件将被使用。

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

所以一定要贯彻落实<一个href=\"http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring\"><$c$c>__toString()在你的类,它的方法平等角色输出相同的值,例如

So make sure to implement the __toString() method in your class and that it outputs the same value for equal roles, e.g.

class Role {
    private $name;

    //.....

    public function __toString() {
        return $this->name
    }

}

这会考虑两个角色作为平等的,如果他们有相同的名称。

This would consider two roles as equal if they have the same name.

这篇关于array_unique的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆