从simpleXMLElement数组中获取特定值

时间:2012-08-24 20:31:22

标签: php arrays simplexml

我见过类似的问题,但没有一个成功回答。我有这个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]。

我尝试过很多版本的循环来获取密钥名称。我只是想不出来。请帮忙!

1 个答案:

答案 0 :(得分:0)

SimpleXML对象可以像数组一样对待。只需循环访问并获取所需的数据。

您可以使用->代替[]来访问他们的数据。

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

DEMO:http://codepad.viper-7.com/Ncn3C1