2011-02-18 139 views
16

有没有像in_array这样的功能,但可以用在物体上?in_array - 'in_object'等效?

+0

为什么你需要这样做?对象不应该被视为数组。 – BoltClock 2011-02-18 13:00:37

+0

怎么样处理SimpleXML对象或者干脆值的对象?建议如何查看对象中是否存在值? – 2011-10-12 21:13:16

回答

22

不,但可以把对象到一个数组,并将它传递到in_array()

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

虽然违反了各种OOP原则。看到我对你的问题和Aron's answer的评论。

6

你可以投的对象数组:

$obj = new stdClass(); 
$obj->var = 'foobar'; 
in_array('foobar', (array)$obj); // true 
12

首先,arraysobjects相当不同。

甲PHP对象不能被通过像阵列迭代,默认情况下。实现对象迭代的一种方式是实现Iterator接口。

关于你的具体问题,你可能想看看在ArrayAccess interface

class obj implements ArrayAccess { 
    private $container = array(); 
    public function __construct() { 
     $this->container = array(
      "one" => 1, 
      "two" => 2, 
      "three" => 3, 
     ); 
    } 
    public function offsetSet($offset, $value) { 
     if (is_null($offset)) { 
      $this->container[] = $value; 
     } else { 
      $this->container[$offset] = $value; 
     } 
    } 
    public function offsetExists($offset) { 
     return isset($this->container[$offset]); 
    } 
    public function offsetUnset($offset) { 
     unset($this->container[$offset]); 
    } 
    public function offsetGet($offset) { 
     return isset($this->container[$offset]) ? $this->container[$offset] : null; 
    } 
} 

现在,您可以访问你的对象就像在下列方式排列:

​​

之前你走在这个疯狂的,虽然,请考虑为什么你实际上是试图做到这一点并看看在php.net的例子。

选项2:当你只是想看到,如果一个属性存在,您可以使用property_exists()此:

class foo { 
    public $bar = 'baz'; 
} 

$object = new foo(); 
var_dump(property_exists($object, 'bar')); // true 
2

我不建议这样做,因为这是非常不好的做法但您可以使用get_object_vars

根据范围获取给定对象的可访问的非静态属性。

还有其他的限制,你应该参考文档,看看它是否适合你。

if(in_array('find me', get_object_vars($obj))) 
5
function in_object($needle, $haystack) { 
    return in_array($needle, get_object_vars($haystack)); 
} 
3

这是令人难以置信所有的人怎么会错过的in_object PHP方法的有效性的地步!这是我想出的,这是非常有用的,你会明白为什么!

下面是一个简单的函数,我写这将检查是否值可以在对象中被发现。

<?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; 
    } 
?> 

如果对象已经创建,这是非常有用的动态(特别是来自外部代码,它不控制,如在应用程序的插件,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; 
?> 

只要运行它自己看!

0

这是最有效和最正确的解决方案。通过一些修改,它可以用于检查任何对象中存在的任何数据类型。

if(gettype($object->var1->var2) == "string"){ 
echo "Present"; 
}