我有各种各样的对象:
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]值的天气,我该怎么做呢?
答案 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