0

I'm asking for explanation of the following behavior behind the scene in PHP.

I have a SimpleXMLElement Object that I print_r(); Here is the result:

Array
(
    [Shipments] => SimpleXMLElement Object
        (
            [Shipment] => SimpleXMLElement Object
                (
                    [Rates] => SimpleXMLElement Object
                        (
                            [Rate] => SimpleXMLElement Object
                                (
                                    [Service] => C
                                    [ServiceCharge] => 10.18
                                    [ServiceChargeDetails] => SimpleXMLElement Object
                                        (
                                            [BaseCharge] => 8.33
                                            [CODCharge] => 0
                                            [DeclaredCharge] => 0
                                            [AdditionalCharges] => 1.85
                                            [SaturdayCharge] => 0
                                        )

                                    [FuelCharge] => 0
                                    [TotalCharge] => 10.18
                                    [BilledWeight] => 6
                                    [TransitDays] => 1
                                    [ExpectedDeliveryDate] => 20160121
                                    [CommitTime] => 17:00:00
                                    [RateZone] => 2
                                    [GlobalRate] => 10.66
                                )

                        )

                    [UID] => ID1
                    [Delzip] => 95126
                    [PUZip] => 95035
                    [Declared] => 100
                    [Residential] => true
                    [COD] => 0
                    [SaturdayDel] => false
                    [Weight] => 2
                    [DIM] => SimpleXMLElement Object
                        (
                            [Length] => 10
                            [Width] => 12
                            [Height] => 13
                        )

                    [Error] => SimpleXMLElement Object
                        (
                        )

                )

            [Error] => SimpleXMLElement Object
                (
                )

        )

)

Please take a look at the "TotalCharge". It doesn't seem to have any child elements however when I do the following:

print_r($response['Shipments']->Shipment->Rates->Rate->TotalCharge);

Here is what I get:

SimpleXMLElement Object
(
    [0] => 10.18
)

I'm trying to understand how did the [0] get there and why the initial print_r didn't show it?

Thanks!

user1029829
  • 941
  • 3
  • 16
  • 34
  • that's the way it parses it. Just use it by adding [0] to the code :) – Gavriel Jan 21 '16 at 01:02
  • I know how to access it, I'm trying to understand how that works. It is print_r kind of parsing it in a shallow way? – user1029829 Jan 21 '16 at 01:07
  • 2
    Don't use `print_r` with `SimpleXmlElement`, it will trick you, http://stackoverflow.com/a/3109364/4333555; http://stackoverflow.com/a/12437393/4333555 – chris85 Jan 21 '16 at 01:08

0 Answers0