ballhogjoni Posted December 4, 2010 Share Posted December 4, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220667-simplexmlelement-object-help/ Share on other sites More sharing options...
AbraCadaver Posted December 4, 2010 Share Posted December 4, 2010 The first way doesn't work because advertiser-id is an object property not an array index. The second way doesn't work because PHP variables cannot contain a dash (-). Try this: echo $o->{"advertiser-id"}; Quote Link to comment https://forums.phpfreaks.com/topic/220667-simplexmlelement-object-help/#findComment-1142963 Share on other sites More sharing options...
salathe Posted December 4, 2010 Share Posted December 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220667-simplexmlelement-object-help/#findComment-1142964 Share on other sites More sharing options...
ballhogjoni Posted December 4, 2010 Author Share Posted December 4, 2010 awesome! thanks Quote Link to comment https://forums.phpfreaks.com/topic/220667-simplexmlelement-object-help/#findComment-1143011 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.