我有各种看起来像这样的对象:
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] 值,我该怎么做?
最佳答案
检查对象中是否存在 name
属性:
if(isset($obj->name)) {
// It exists!
}
所以,如果你想找到那些具有 $name
属性的对象:
$result = array_filter($myArray, function($x) {
return isset($x->name);
}); // Assuming PHP 5.3 or higher
关于php - 如何搜索对象以查看它们是否包含特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10115480/