PHP in_array object comparing?

后端 未结 8 1190
终归单人心
终归单人心 2020-12-17 08:37

Can the in_array function compare objects?

For example, I have an array of objects and I want to add them distinctly to another array. Is it possible t

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 09:40

    See http://php.net/manual/en/function.spl-object-hash.php

    if ( ! array_key_exists( spl_object_hash( $obj ), $objects ) ) {
        $objects[ spl_object_hash( $obj ) ] = $obj;
    }
    

    Cheers

提交回复
热议问题