1

I am trying to read an XML output listed below:

SimpleXMLElement Object
(
   [merchant] => SimpleXMLElement Object
        (
            [merchant-id] => SimpleXMLElement Object
                (
                )
            [merchant-name] => Snip-Its
            [merchant-category] => Hair
            [merchant-website] => www.snipits.com
        )

    [store] => SimpleXMLElement Object
        (
            [store-id] => 173DFBB7-67F6-DE11-AD5B-0026B953348A
            [store-name] => Heb
            [store-number] => 0109
        )

    [coupon] => SimpleXMLElement Object
        (
            [coupon-id] => 385748
            [coupon-address] => 14096 Memorial Drive
            [coupon-city] => Houston
            [coupon-state] => TX
        )
)

using PHP script as follows but I get a value of '0':

foreach( $data->coupons as $mycoupon ) {    
      $address = $mycoupon->coupon->coupon-address;     
    //print_r($mycoupon); 
}

So, please help me to understand how to read the values of SimpleXMLElement Object and assign them to a local variable.

1 Answer 1

1

There are certain characters that are not permitted for PHP's naming convention such as a hyphen. To deal with this just enclose the element name in braces as a string as such:

$address = $mycoupon->coupon->{'coupon-address'};

Give that a try.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.