0

我见过类似的问题,但没有一个成功回答。我有这个 simpleXMLElement 转数组:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [uri] => https://66.90.34.19:9630/api/users/2/
                    [id] => 2
                    [full_render] => true
                )

            [username] => Admin
            [password] => SimpleXMLElement Object
                (
                )

            [name] => SimpleXMLElement Object
                (
                    [first] => LightSpeed
                    [last] => Administrator
                )

            [email] => SimpleXMLElement Object
                (
                )

            [account_locked] => false
            [privilege_group] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [uri] => https://66.90.34.19:9630/api/privilege_groups/0/
                            [id] => 0
                            [full_render] => false
                        )

                    [name] => Administrator
                    [type] => administrator
                )

            [read_eula] => true
            [hidden] => false
            [enabled] => true
            [phone] => SimpleXMLElement Object
                 (
                 )

            [product] => SimpleXMLElement Object
                (
                )

            [product_code] => SimpleXMLElement Object
                (
                )

            [open_to_pos] => false
            [gsx_tech_id] => SimpleXMLElement Object
                 (
                )

            [can_open_from_otr] => true
            [can_discount] => true
            [internal_user] => false
            [active] => true
            [expired] => false
        )

    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [uri] => https://66.90.34.19:9630/api/users/2/
                    [id] => 2
                    [full_render] => true
                )

            [username] => Admin
            [password] => SimpleXMLElement Object
                (
                )

             [name] => SimpleXMLElement Object
                (
                    [first] => LightSpeed
                    [last] => Administrator
                )

            [email] => SimpleXMLElement Object
                (
                )

            [account_locked] => false
            [privilege_group] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [uri] => https://66.90.34.19:9630/api/privilege_groups/0/
                            [id] => 0
                            [full_render] => false
                        )

                    [name] => Administrator
                    [type] => administrator
                )

            [read_eula] => true
            [hidden] => false
            [enabled] => true
            [phone] => SimpleXMLElement Object
                (
                )

            [product] => SimpleXMLElement Object
                (
                )

            [product_code] => SimpleXMLElement Object
                (
                )

            [open_to_pos] => false
            [gsx_tech_id] => SimpleXMLElement Object
                 (
                 )

            [can_open_from_otr] => true
            [can_discount] => true
            [internal_user] => false
            [active] => true
            [expired] => false
        )

我想要回显 [name]->[first] 和 [name]->[last]。

我尝试了许多版本的循环来通过键名获取它们。我就是想不通。请帮忙!

4

1 回答 1

0

SimpleXML 对象可以被视为数组。只需循环并获取您需要的数据。

您使用->而不是访问他们的数据[]

foreach($array as $x){
    $name = $x->name;
    echo $name->first . ' ' . $name->last;
}

演示:http ://codepad.viper-7.com/Ncn3C1

于 2012-08-24T20:38:39.080 回答