如何搜索对象以查看它们是否包含特定值?

时间:2012-04-11 23:38:25

标签: php drupal search object

我有各种各样的对象:

Array
(
[0] => stdClass Object
    (
        [tid] => 18
        [vid] => 1
        [name] => test
        [description] => 
        [format] => 
        [weight] => 0
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

[1] => stdClass Object
    (
        [tid] => 21
        [vid] => 1
        [name] => tag
        [description] => 
        [format] => 
        [weight] => 0
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )
)

基本上我需要找出这些天体中存在[name]值的天气,我该怎么做呢?

1 个答案:

答案 0 :(得分:6)

检查对象中是否存在name属性:

if(isset($obj->name)) {
    // It exists!
}

因此,如果要查找具有$name属性的对象:

$result = array_filter($myArray, function($x) {
    return isset($x->name);
}); // Assuming PHP 5.3 or higher