通过数组访问对象属性


Access object property via array

如何简化这个结构:

$properties = $rs->$domains[0];
$dn = $properties[0]->distinguishedname;

因为PHP在这样的构造上给出错误:

$dn = $rs->$domains[0][0]->distinguishedname

这应该可以工作,或者更确切地说,它们都不应该工作。

使用->操作符访问一个对象的成员,你不需要使用$两次,据我所知,它应该抛出一个错误或某种警告。

$properties = $rs->$domains[0];
$dn = $properties[0]->distinguishedname;
应:

$properties = $rs->domains[0];
$dn = $properties[0]->distinguishedname;
同样

:

$dn = $rs->$domains[0][0]->distinguishedname;
应:

$dn = $rs->domains[0][0]->distinguishedname;

只看它快速,似乎你的代码应该工作,只要数据实际上在那里。也许您尝试从没有属性的中获取 distinguhedname ?

php错误说什么?在您的数据结构上执行*var_dump*,以查看它实际上包含了您期望的数据。