PHP SimpleXML对象属性存在为数组,但在直接访问时变为对象

时间:2013-11-20 23:06:19

标签: php arrays object simplexml

我在尝试解析Authorize.Net的CIM API的响应时遇到了一个奇怪的问题。 PHP使用API​​的响应来创建SimpleXML对象。对象的属性报告为数组(并且因为它包含2个唯一索引),但是当直接访问该属性时,print_r(),gettype()和is_array()都将其报告/视为对象,而不是数组。

以下PHP代码:

print_r($parsedresponse);
print_r($parsedresponse->profile);
print_r($parsedresponse->profile->paymentProfiles);

生成以下输出:     SimpleXMLElement对象

(

    [messages] => SimpleXMLElement Object

        (

            [resultCode] => Ok

            [message] => SimpleXMLElement Object

                (

                    [code] => I00001

                    [text] => Successful.

                )

        )
    [profile] => SimpleXMLElement Object

        (

            [merchantCustomerId] => 1

            [description] => Customer Profile for Store Code MC Created 11/20/13

            [email] => email@email.com

            [customerProfileId] => 11111111

            [paymentProfiles] => Array

                (

                    [0] => SimpleXMLElement Object

                        (

                            [customerType] => individual

                            [billTo] => SimpleXMLElement Object

                                (

                                    [firstName] => Test

                                    [lastName] => Test

                                    [company] => Company Name

                                    [address] => 1234 Test St

                                    [city] => Testlyvania
                                    [state] => OH

                                    [zip] => 44720

                                    [country] => United States

                                    [phoneNumber] => 123-456-7890

                                    [faxNumber] => SimpleXMLElement Object

                                        (

                                        )

                                )

                            [customerPaymentProfileId] => 22222222

                            [payment] => SimpleXMLElement Object

                                (

                                    [creditCard] => SimpleXMLElement Object

                                        (

                                            [cardNumber] => XXXX1111

                                            [expirationDate] => XXXX

                                        )

                                )

                        )

                    [1] => SimpleXMLElement Object

                        (

                            [customerType] => individual

                            [billTo] => SimpleXMLElement Object

                                (

                                    [firstName] => Test

                                    [lastName] => Test

                                    [company] => SimpleXMLElement Object
                                        (
                                        )

                                    [address] => 1234 Test St

                                    [city] => Testlyvania

                                    [state] => OH

                                    [zip] => 44720

                                    [country] => United States

                                    [phoneNumber] => 1234567890

                                    [faxNumber] => 123-456-7890
                                )

                            [customerPaymentProfileId] => 33333333

                            [payment] => SimpleXMLElement Object

                                (

                                    [creditCard] => SimpleXMLElement Object

                                        (

                                            [cardNumber] => XXXX0002

                                            [expirationDate] => XXXX

                                        )

                                )


                        )

                )

            [shipToList] => Array
                (

                    [0] => SimpleXMLElement Object

                        (

                            [firstName] => Test

                            [lastName] => Test

                            [company] => Company Name

                            [address] => 1234 Test St

                            [city] => Testlyvania

                            [state] => OH

                            [zip] => 44720

                            [country] => United States

                            [phoneNumber] => 123-456-7890

                            [faxNumber] => SimpleXMLElement Object

                                (

                                )

                            [customerAddressId] => 44444444
                        )

                    [1] => SimpleXMLElement Object
                        (

                            [firstName] => Test

                            [lastName] => Test

                            [company] => SimpleXMLElement Object

                                (

                                )

                            [address] => 1234 Test St

                            [city] => Testlyvania

                            [state] => OH

                            [zip] => 44720

                            [country] => United States

                            [phoneNumber] => 1234567890

                            [faxNumber] => 123-456-7890

                            [customerAddressId] => 55555555
                        )

                )

        )

)


SimpleXMLElement Object

(

    [merchantCustomerId] => 1

    [description] => Customer Profile for Store Code MC Created 11/20/13

    [email] => email@email.com

    [customerProfileId] => 11111111

    [paymentProfiles] => Array

        (

            [0] => SimpleXMLElement Object

                (

                    [customerType] => individual

                    [billTo] => SimpleXMLElement Object

                        (

                            [firstName] => Test

                            [lastName] => Test

                            [company] => Company Name

                            [address] => 1234 Test St

                            [city] => Testlyvania

                            [state] => OH

                            [zip] => 44720

                            [country] => United States

                            [phoneNumber] => 123-456-7890

                            [faxNumber] => SimpleXMLElement Object

                                (

                                )

                        )

                    [customerPaymentProfileId] => 22222222

                    [payment] => SimpleXMLElement Object

                        (

                            [creditCard] => SimpleXMLElement Object

                                (

                                    [cardNumber] => XXXX1111

                                    [expirationDate] => XXXX
                                )

                        )

                )

            [1] => SimpleXMLElement Object

                (

                    [customerType] => individual

                    [billTo] => SimpleXMLElement Object

                        (

                            [firstName] => Test

                            [lastName] => Test

                            [company] => SimpleXMLElement Object

                                (

                                )

                            [address] => 1234 Test St

                            [city] => Testlyvania

                            [state] => OH

                            [zip] => 44720

                            [country] => United States

                            [phoneNumber] => 1234567890

                            [faxNumber] => 123-456-7890

                        )

                    [customerPaymentProfileId] => 33333333

                    [payment] => SimpleXMLElement Object

                        (

                            [creditCard] => SimpleXMLElement Object

                                (

                                    [cardNumber] => XXXX0002

                                    [expirationDate] => XXXX

                                )

                        )

                )

        )

    [shipToList] => Array

        (

            [0] => SimpleXMLElement Object

                (

                    [firstName] => Test

                    [lastName] => Test

                    [company] => Company Name

                    [address] => 1234 Test St

                    [city] => Testlyvania

                    [state] => OH

                    [zip] => 44720

                    [country] => United States

                    [phoneNumber] => 123-456-7890

                    [faxNumber] => SimpleXMLElement Object

                        (

                        )

                    [customerAddressId] => 44444444
                )

            [1] => SimpleXMLElement Object

                (

                    [firstName] => Test

                    [lastName] => Test

                    [company] => SimpleXMLElement Object

                        (

                        )

                    [address] => 1234 Test St

                    [city] => Testlyvania

                    [state] => OH

                    [zip] => 44720

                    [country] => United States

                    [phoneNumber] => 1234567890

                    [faxNumber] => 123-456-7890

                    [customerAddressId] => 55555555
                )

        )

)


SimpleXMLElement Object

(

    [customerType] => individual

    [billTo] => SimpleXMLElement Object

        (

            [firstName] => Test

            [lastName] => Test

            [company] => Company Name

            [address] => 1234 Test St

            [city] => Testlyvania

            [state] => OH

            [zip] => 44720

            [country] => United States

            [phoneNumber] => 123-456-7890

            [faxNumber] => SimpleXMLElement Object

                (

                )

        )

    [customerPaymentProfileId] => 22222222

    [payment] => SimpleXMLElement Object

        (

            [creditCard] => SimpleXMLElement Object

                (

                    [cardNumber] => XXXX1111

                    [expirationDate] => XXXX

                )

        )

)

正如您所看到的,一旦直接访问“paymentProfiles”属性,它就会从Array类型变为Object类型。但是,如果我试图直接访问它的第二个索引,那就可以了。

PHP代码:

print_r($parsedresponse->profile->paymentProfiles[1])

产生以下输出:

SimpleXMLElement Object

(

    [customerType] => individual

    [billTo] => SimpleXMLElement Object

        (

            [firstName] => Test

            [lastName] => Test

            [company] => SimpleXMLElement Object

                (

                )

            [address] => 1234 Test St

            [city] => Testlyvania

            [state] => OH

            [zip] => 44720

            [country] => United States

            [phoneNumber] => 1234567890

            [faxNumber] => 123-456-7890

        )

    [customerPaymentProfileId] => 33333333

    [payment] => SimpleXMLElement Object

        (

            [creditCard] => SimpleXMLElement Object

                (

                    [cardNumber] => XXXX0002

                    [expirationDate] => XXXX

                )


        )

)

为何直接访问$ parsedresponse-> profile-> paymentProfiles的行为类似于对象而非预期的数组?

1 个答案:

答案 0 :(得分:0)

其他答案似乎包含您需要的大部分信息。它帮助了我:Why does SimpleXML change my array to the array's first element when I use it?。 foreach循环对我来说效果很好,尽管循环标准也适用。

因此,您应该能够执行以下操作,即使$ parsedresponse->配置文件不是根据print_r的数组。如果$ parsedresponse-> profile是一个数组,它也应该有用。

print_r($parsedresponse);
foreach($profile as $prof){
    print_r($prof);
    foreach($prof->paymentProfiles as $pProf){
        print_r($pProf);
    }
}