Jump to content

SimpleXMLElement Object help


ballhogjoni

Recommended Posts

I have this SimpleXMLElement Object and I can't echo out any attribute. In this example I am only showing you one SimpleXMLElement Object of the entire result set just for brevity. Here is the code that loops through the results:

foreach($oXML->links->link as $o){
    echo "<pre>";print_r($o);echo "</pre>";
}

 

SimpleXMLElement Object
(
    [advertiser-id] => 2860239
    [advertiser-name] => Amber Alert GPS
    [category] => Array
        (
            [0] => children
            [1] => children
        )

    [click-commission] => Array
        (
            [0] => 0.0
            [1] => 0.0
        )

    [creative-height] => Array
        (
            [0] => 0
            [1] => 0
        )

    [creative-width] => Array
        (
            [0] => 0
            [1] => 0
        )

    [language] => en
    [lead-commission] => SimpleXMLElement Object
        (
        )

    [link-code-html] => "Hey Mom! Please Don't Lose Me Again." Use Promo Code "Amber2" To Get 20% Off New GPS Child Tracking Device!
    [link-code-javascript] => 
"Hey Mom! Please Don't Lose Me Again." Use Promo Code "Amber2" To Get 20% Off New GPS Child Tracking Device!

    [description] => 20% discount of Amber Alert GPS Child Locator Device when using "Amber2" Promo Code.
    [destination] => http://www.amberalertgps.com
    [link-id] => 10745524
    [link-name] => "Hey Mom! Please Don't Lose Me Again." Use Promo Code "Amber2" To Get 20% Off New GPS Child Tracking Device!
    [link-type] => Text Link
    [performance-incentive] => false
    [promotion-end-date] => SimpleXMLElement Object
        (
        )

    [promotion-start-date] => SimpleXMLElement Object
        (
        )

    [promotion-type] => coupon
    [relationship-status] => joined
    [sale-commission] => 30.00%
    [seven-day-epc] => N/A
    [three-month-epc] => 2.66
)

 

I try to do this but it doesn't work:

foreach($oXML->links->link as $o){
    echo $o['advertiser-id'];
}

# I also tried this:
foreach($oXML->links->link as $o){
    echo $o->advertiser-id;
}

 

How do you echo the values?

Link to comment
https://forums.phpfreaks.com/topic/220667-simplexmlelement-object-help/
Share on other sites

Those values are not attributes in the usual sense. Attributes are part of an element's opening tag and look like blah="value" in the XML.  With SimpleXML, they would be visible when using print_r as items within an @attributes property, but none of this is useful to you.

 

The main problem is that your element name (advertiser-id) is not a valid object property label in PHP so, as you've seen, you cannot access it like $o->advertiser-id since PHP interprets that as an arithmetic operation $o->advertiser minus id.  To access properties containing invalid characters you have to use a special syntax (this is not particular to SimpleXML, the same goes for any object) like $o->{'advertiser-id'}.

 

An example of this is given in the PHP manual's "Basic usage" of SimpleXML... see Example #3 on http://php.net/simplexml.examples-basic

 

Edit - Ahh, too slow. Well, you've gotten the answer twice now. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.