Php in_数组-';in#u object';相等的
在数组中是否有类似于Php in_数组-';in#u object';相等的,php,function,Php,Function,在数组中是否有类似于的函数,但可以用于对象?没有,但可以将对象强制转换为数组,并将其传递到数组()中的 但这违反了所有OOP原则。请参阅我对您的问题的评论。您可以将对象强制转换为数组: $obj = new stdClass(); $obj->var = 'foobar'; in_array( 'foobar', (array)$obj ); // true 首先,它们是完全不同的。 默认情况下,PHP对象不能像数组一样迭代。实现对象迭代的一种方法是实现迭代器接口 关于您的具体问题,您可
的函数,但可以用于对象?没有,但可以将对象强制转换为数组,并将其传递到数组()中的
但这违反了所有OOP原则。请参阅我对您的问题的评论。您可以将对象强制转换为数组:
$obj = new stdClass();
$obj->var = 'foobar';
in_array( 'foobar', (array)$obj ); // true
首先,它们是完全不同的。
默认情况下,PHP对象不能像数组一样迭代。实现对象迭代的一种方法是实现迭代器接口
关于您的具体问题,您可能想看看:
现在,您可以通过以下方式像数组一样访问对象:
$object = new obj();
var_dump(isset($obj['two'])); // exists!
var_dump(isset($obj['foo'])); // does not exist
<强>虽然在这之前你会发疯,但请考虑为什么你真的要这么做,看看PHP.NET中的例子。< /P>
选项2:当您只是试图查看某个属性是否存在时,您可以使用:
class foo {
public $bar = 'baz';
}
$object = new foo();
var_dump(property_exists($object, 'bar')); // true
我不推荐它,因为这是一种非常糟糕的做法,但你可以使用它
根据作用域获取给定对象的可访问非静态属性
还有其他一些限制,您应该参考文档,看看它是否适合您
if(in_array('find me', get_object_vars($obj)))
难以置信所有人都忽视了in_对象PHP方法的实用性!这是我提出的,它非常有用,你会明白为什么
下面是我编写的一个简单函数,它将检查是否可以在对象中找到值
如果对象是动态创建的(尤其是从外部代码创建的,而您不控制外部代码,如在应用程序插件、CMS等中),并且您不知道对象的属性,则这非常有用。
上面的函数将返回属性,因此您以后可以在代码中使用它
下面是一个非常好的基本示例,说明此函数是多么有用
<?php
class My_Class {
function __construct($key, $value) {
$this->$key = $value;
// As you can see, this is a dynamic class, its properties and values can be unknown...
}
}
function in_object($value,$object) {
if (is_object($object)) {
foreach($object as $key => $item) {
if ($value==$item) return $key;
}
}
return false;
}
function manipulate_property($value,$object) {
if ($property = in_object($value,$object)) {
// value found. I can now use this property.
// I can simply echo'it (makes no sense, as I could instead simply echo "value")
echo "<br />I found the property holding this value: ".$object->$property;
// or (here comes the good part)
// change the property
$object->$property = "This is a changed value!";
echo "<br />I changed the value to: ".$object->$property;
// or return it for use in my program flow
return $property;
} else {
echo "Value NOT FOUND!<br />";
return false;
}
}
// imagine if some function creates the class conditionally...
if ( 1 == 1) {
$class = new My_Class("property","Unchanged Value");
} else {
$class = new My_Class("property","Some Other Value");
}
// now let's check if the value we want exists, and if yes, let's have some fun with it...
$property = manipulate_property("Unchanged Value",$class);
if ($property) {
$my_variable = $class->$property;
echo "<br />This is my variable now:".$my_variable;
} else $my_variable = $some_other_variable;
?>
这是最有效、最正确的解决方案。经过一些修改,它可以应用于检查任何对象中存在的任何数据类型
if(gettype($object->var1->var2) == "string"){
echo "Present";
}
你为什么要这么做?对象不应被视为数组。处理SimpleXML对象或简单的值对象怎么样?查看对象中是否存在值的建议方法是什么?如果有人正在查找数字0
,并希望获得布尔值,该怎么办?
function in_object($needle, $haystack) {
return in_array($needle, get_object_vars($haystack));
}
<?php
// in_object method
// to check if a value in an object exists.
function in_object($value,$object) {
if (is_object($object)) {
foreach($object as $key => $item) {
if ($value==$item) return $key;
}
}
return false;
}
?>
<?php
class My_Class {
function __construct($key, $value) {
$this->$key = $value;
// As you can see, this is a dynamic class, its properties and values can be unknown...
}
}
function in_object($value,$object) {
if (is_object($object)) {
foreach($object as $key => $item) {
if ($value==$item) return $key;
}
}
return false;
}
function manipulate_property($value,$object) {
if ($property = in_object($value,$object)) {
// value found. I can now use this property.
// I can simply echo'it (makes no sense, as I could instead simply echo "value")
echo "<br />I found the property holding this value: ".$object->$property;
// or (here comes the good part)
// change the property
$object->$property = "This is a changed value!";
echo "<br />I changed the value to: ".$object->$property;
// or return it for use in my program flow
return $property;
} else {
echo "Value NOT FOUND!<br />";
return false;
}
}
// imagine if some function creates the class conditionally...
if ( 1 == 1) {
$class = new My_Class("property","Unchanged Value");
} else {
$class = new My_Class("property","Some Other Value");
}
// now let's check if the value we want exists, and if yes, let's have some fun with it...
$property = manipulate_property("Unchanged Value",$class);
if ($property) {
$my_variable = $class->$property;
echo "<br />This is my variable now:".$my_variable;
} else $my_variable = $some_other_variable;
?>
if(gettype($object->var1->var2) == "string"){
echo "Present";
}