我有一个定义如下的对象:
$userAttributes->deviceInfo->macAddress->type = "text";
$userAttributes->deviceInfo->macAddress->required = "*";
$userAttributes->deviceInfo->macAddress->options = NULL;
$userAttributes->deviceInfo->macAddress->size = "16";
$userAttributes->callControl->guest->isActive->type = "select";
$userAttributes->callControl->guest->isActive->required = "*";
$userAttributes->callControl->guest->isActive->options = $tF;
$userAttributes->callControl->guest->isActive->size = NULL;
如果我这样做:
foreach ($userAttributes as $key => $value)
{
foreach ($value as $k => $v)
{
echo $v->type;
echo $v->required;
echo $v->options;
echo $v->size;
}
}
当它到达callControl片段时会失败。我如何确定$ v->有孩子?属性“isActive”可以是任意数量的东西,所以我无法做任何事情并做$ v-> isActive-> Blah
我如何确定$ v->有孩子,如果是这样的话,那就过去了?
答案 0 :(得分:2)
PHP有一个内置函数来检查某些东西是否是一个对象。它被称为is_object
,它会检查传递给它的参数是否为对象。
进一步阅读:http://php.net/manual/es/function.is-object.php
对于您的特定情况,您可以创建一个函数来递归调用它:
function getAttributes($obj){
foreach($obj as $k => $v){
if(is_object($v)){
getAttributes($v);
}else{
echo $v;
}
}
}