欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

in_array()函数第三个参数的问题

程序员文章站 2022-05-03 13:21:07
...
class Included{}class test{    public $arr = array();    function a(Included $include){        if (in_array($include, $this->arr, true)){        	  return;        }        $this->arr[] = $include;    }}$test = new test();$test->a(new Included());$test->a(new Included());$test->a(new Included());print_r($test->arr);   //   输出Array ( [0] => Included Object ( ) [1] => Included Object ( ) [2] => Included Object ( ) )

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。
这个类型是否可以理解为对象实例。 因为上面的例子中的in_array()第三个参数默认的时候,只有输出
Array ( [0] => Included Object ( ) )



回复讨论(解决方案)

是的,这个类型同样包括对象实例

恩,谢谢您了。

第三???是?查是否相同?型的。
例如:

谢谢楼上,但是这个类型相同是否是包含对象实例,这个php手册里没有这方面的描述。

手册中是这样描述的
如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的 类型是否和 haystack 中的相同。

那个类型是个链接,点开后是

这就不需要再细说了吧?
而况你实测的结果也是包括对象的

好的,谢谢了。