5

我有各种看起来像这样的对象:

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] 值的天气,我该怎么做呢?

4

1 回答 1

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
于 2012-04-11T23:48:29.427 回答