簡體   English   中英

如何訪問PHP SimpleXMLElement屬性?

[英]How to access PHP SimpleXMLElement attributes?

這是我的xml對象。 我無法在腳本的對象數組中對此進行解析。

SimpleXMLElement Object(
[pagination] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [pageNumber] => 1
                [pageSize] => 100
                [totalAvailable] => 1
            )

    )

[users] => SimpleXMLElement Object
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => xxxxxxxx
                        [name] => xxxxx
                        [siteRole] => xxxxx
                        [lastLogin] => xxxxxx
                        [externalAuthUserId] =>
                    )
            )
    )
)

這是我的PHP代碼:

foreach($xml2->users as $item) { 
$userName = $item->attributes()->name;
}

難道我做錯了什么?

我能夠解決問題。 這是修改后的代碼:

foreach($xml2->users[0] as $user) { 
    $userName = $user['name'];
}

多虧了ymas,他正確地指出了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM