访问stdClass对象中的属性

时间:2013-10-10 00:05:06

标签: php arrays object stdclass

stdClass Object
(
[ModuleAccountInfo] => Array
    (
        [0] => stdClass Object
            (
                [ServerName] => EAST
                [HostingModule] => ActiveDirectory
                [ActiveDirectorySiteName] => EAST
                [AccountIdentity] => OU=ndla,OU=Hosting,DC=east,DC=domain,DC=local
                [Groups] => 2
                [Users] => 15
            )

        [1] => stdClass Object
            (
                [ServerName] => EAST.net
                [HostingModule] => hange
                [DiskQuota] => 375000
                [DiskQuotaAdditional] => 0
                [DateQuotaExceeded] => 0001-01-01T00:00:00
                [DiskSpace] => 58567
                [MailboxesDiskSpace] => 59973051
                [PublicFoldersDiskSpace] => 0
                [MessageArchivingDiskSpace] => 0
                [Contacts] => 8
                [Mailboxes] => 15

如何访问ServerName属性? 该对象保存在$ modules变量中。以上是$ modules的print_r。

1 个答案:

答案 0 :(得分:10)

因为ModuleAccountInfo属性是一个数组,所以你需要使用特定的索引

echo $modules->ModuleAccountInfo[0]->ServerName;

或循环

foreach ($modules->ModuleAccountInfo as $moduleAccountInfo) {
    echo $moduleAccountInfo->ServerName;
}