in_array - 'in_object'等效? [英] in_array - 'in_object' equivalent?

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

问题描述

有没有像 in_array 这样的函数,但可以用在对象上?

Is there such a function like in_array, but can be used on objects?

推荐答案

不可以,但是你可以将这个对象转换为一个数组并将它传递给 in_array()

Nope, but you can cast the object to an array and pass it into in_array().

$obj = new stdClass;
$obj->one = 1;
var_dump(in_array(1, (array) $obj)); // bool(true)

这违反了各种OOP原则。看到我对你的问题的评论和阿隆的回答

That violates all kinds of OOP principles though. See my comment on your question and Aron's answer.

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

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