2011-02-18 138 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"; 
}